'How to join two groupby dataframes in Python?

I would like to make a left join on a groupby result with another groupby result:

group1=table1.groupby(['Country','Year','Adjustment'],as_index=False)
group2=table2.groupby(['location','year','cause',as_index=False)
result=pd.merge(group1,group2,left_on=['Country','Year','Adjustment'],right_on=['location','year','cause'],how='left')

this is the error I get:

can not merge DataFrame with instance of type class 'pandas.core.groupby.groupby.DataFrameGroupBy'

I did not find a solution online. I found a similar question where the solution suggests to use to_frame(), but it did not work.

Thanks a lot in advance.

Ron



Solution 1:[1]

To concatenate 2 dataframes you us pd.concat() function. try:

pd.concat([group1,group2], axis=0)

axis = 0 or axis = 1 as per your choice.. axis = 1 will append data as columns. axis = 0 will append data as rows

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s