Reputation: 1
My question is about how to join 2 dataframes, which were created by the groupby
method and the sum()
and max()
functions.
The case is: I have 1st dataframe with next structure:
And the second one with the same structure, but instead of DepartmentDescripton
... variables, I have "Monday", "Tuesday" etc.
Then I try to join them:
train_joined_dep_week = pd.merge(tr_bin_dep_grouped, tr_bin_weekday_grouped_flag, left_on=["VisitNumber"], right_on=["VisitNumber"], how='INNER')`
the error is
KeyError
Traceback (most recent call last)
<ipython-input-24-faccedccea7d> in <module>()
----> 1 train_joined_dep_week = pd.merge(tr_bin_dep_grouped, tr_bin_weekday_grouped_flag, left_on=["VisitNumber"], right_on=["VisitNumber"], how='INNER')
[...]
KeyError: 'VisitNumber'
Note: I have two different functions for aggregation (sum()
and max()
), so I can't just make 1 dataframe. I believe the problem is about wrong data structure after sum()
func, but I don't understand how to make it in right way.
Upvotes: 0
Views: 205
Reputation: 2291
When you aggregate using groupby
the columns you're grouping by get moved into the index. You should be able to reset the index with tr_bin_dep_grouped.reset_index(inplace=True)
and then merge the two dataframes together.
Upvotes: 1