danfojs icon indicating copy to clipboard operation
danfojs copied to clipboard

How to add, multiply, etc. dataframes considering multi column index?

Open stefaneidelloth opened this issue 2 years ago • 2 comments

Lets say I have two data frames, using two id columns to identify the rows:

A: | id_foo | id_baa | value | | 1 | 1 | 10 | | 1 | 2 | 20 | | 2 | 1 | 30 | | 2 | 2 | 40 |

B: | id_foo | id_baa | value | | 1 | 1 | 100 | | 1 | 2 | 200 | | 2 | 1 | 300 | | 2 | 2 | 400 |

=> What is the recommend way to add those two dataframes, so that the rows are matched by id_foo and id_baa and the entries of the value columns are added?

Expected result:

C: | id_foo | id_baa | value | | 1 | 1 | 110 | | 1 | 2 | 220 | | 2 | 1 | 330 | | 2 | 2 | 440 |

  • I tried to create multi column index with

a.setIndex({columns: ['id_foo', 'id_baa' ], inplace: true, drop: true});

However, setIndex only seems to support a single column (?) https://danfo.jsdata.org/api-reference/dataframe/dataframe.set_index

  • I tried to use the add function, but there does not seem to be an option for specifying index/join columns? https://danfo.jsdata.org/api-reference/dataframe/danfo.dataframe.add

Related: https://github.com/javascriptdata/danfojs/issues/101

stefaneidelloth avatar Sep 08 '22 13:09 stefaneidelloth

Multi

Lets say I have two data frames, using two id columns to identify the rows:

A: | id_foo | id_baa | value | | 1 | 1 | 10 | | 1 | 2 | 20 | | 2 | 1 | 30 | | 2 | 2 | 40 |

B: | id_foo | id_baa | value | | 1 | 1 | 100 | | 1 | 2 | 200 | | 2 | 1 | 300 | | 2 | 2 | 400 |

=> What is the recommend way to add those two dataframes, so that the rows are matched by id_foo and id_baa and the entries of the value columns are added?

Expected result:

C: | id_foo | id_baa | value | | 1 | 1 | 110 | | 1 | 2 | 220 | | 2 | 1 | 330 | | 2 | 2 | 440 |

  • I tried to create multi column index with

a.setIndex({columns: ['id_foo', 'id_baa' ], inplace: true, drop: true});

However, setIndex only seems to support a single column (?) https://danfo.jsdata.org/api-reference/dataframe/dataframe.set_index

  • I tried to use the add function, but there does not seem to be an option for specifying index/join columns? https://danfo.jsdata.org/api-reference/dataframe/danfo.dataframe.add

Related: #101

Multi column index is not supported at the moment

risenW avatar Sep 25 '22 11:09 risenW

Question: does this even work with a single column index at the moment? I.e. is the pandas DataFrame.add behaviour where it will add values from rows by matching on their row index (where there can be holes in one frame or the other) implemented?

edit: oh sorry, I did just notice #101 - so that is explained there.

kitfit-dave avatar Apr 14 '23 13:04 kitfit-dave