Reputation: 7351
Currently, I do
df.groupby('A')['B'].agg({'total' : np.sum})
so that the aggregated column has a column name total
. But recently I get a warning
FutureWarning: using a dict on a Series for aggregation is deprecated and will be removed in a future version
So what is the preferred way now?
Upvotes: 3
Views: 9679
Reputation: 863256
I think you need rename
or define name
parameter in reset_index
:
Check also deprecate groupby agg with a dictionary when renaming.
1.
df = df.groupby('A')['B'].sum().reset_index(name='total')
2.
df = df.groupby('A', as_index=False)['B'].sum().rename(columns={'B':'total'})
3.
df = df.groupby('A').agg({'B' : 'sum'}).rename(columns={'B':'total'}).reset_index()
Upvotes: 3
Reputation: 76967
Here are some ways
In [57]: df.groupby('A')['B'].sum().to_frame('total')
Out[57]:
total
A
a 2
b 1
In [58]: df.groupby('A')['B'].agg(np.sum).to_frame('total') # or agg('sum')
Out[58]:
total
A
a 2
b 1
In [59]: df.groupby('A').agg({'B': np.sum}).rename(columns={'B': 'total'})
Out[59]:
total
A
a 2
b 1
Upvotes: 3