I had a dataframe and did a groupby in FIPS and summed the groups that worked fine.
kl = ks.groupby('FIPS')
kl.aggregate(np.sum)
I just want a normal Dataframe back but I have a pandas.core.groupby.DataFrameGroupBy object.
I had a dataframe and did a groupby in FIPS and summed the groups that worked fine.
kl = ks.groupby('FIPS')
kl.aggregate(np.sum)
I just want a normal Dataframe back but I have a pandas.core.groupby.DataFrameGroupBy object.
df_g.apply(lambda x: x)
will return the original dataframe.
pd.DataFrame(grouped.groups). The GroupBy.apply function apply func to every group and combine them together in a DataFrame.grouped.to_df(). However, after I checked the API of the GroupBy object, I found there wasn't such a function, so I came back to tell everyone this is the easiest way to do that. lol.The result of kl.aggregate(np.sum) is a normal DataFrame, you just have to assign it to a variable to further use it. With some random data:
>>> df = DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
>>> 'foo', 'bar', 'foo', 'foo'],
... 'B' : ['one', 'one', 'two', 'three',
... 'two', 'two', 'one', 'three'],
... 'C' : randn(8), 'D' : randn(8)})
>>> grouped = df.groupby('A')
>>> grouped
<pandas.core.groupby.DataFrameGroupBy object at 0x04E2F630>
>>> test = grouped.aggregate(np.sum)
>>> test
C D
A
bar -1.852376 2.204224
foo -3.398196 -0.045082
AttributeError: Cannot access callable attribute 'info' of 'DataFrameGroupBy' objects, try using the 'apply' method.The cleanest solution is using reset_index().
df = grouped_df.reset_index()
Docs: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.reset_index.html
DataFrameGroupBy to DataFrame. There is no reset_index in DataFrameGroupBy pandas.pydata.org/docs/reference/groupby.html you refer to misleading doc