'Combine GroupBy Object and DataFrame

I have a dataframe and a groupby object that I want to add as columns to the groupby. They are the right shape to fit together, but for some reason don't. I've tried using concat and merge to no avail. df is type <class 'pandas.core.groupby.generic.DataFrameGroupBy'> months_since_df is type <class 'pandas.core.frame.DataFrame'>

df_fin = pd.concat([df, months_since_df], axis=1, join="inner")
df_fin = pd.merge(df, months_since, left_index=True, right_index=True)

When I do this I keep getting the following error.

TypeError: Can only merge Series or DataFrame objects, a <class 'pandas.core.groupby.generic.DataFrameGroupBy'> was passed"

Ultimately my goal is to add months_since_df column to df. This is the first time I'm using groupby, is there something basic I'm missing (sure feels like it.) happy to clarify and add additional info if needed. Thanks!



Sources

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

Source: Stack Overflow

Solution Source