Aggregation functions reduce the dimensionality of returned objects. Some common aggregation functions include mean(), sum(), size(), count(), std(), var(), and sem().
df1 = df.groupby(['A', 'B'], as_index=False)['C'].sum()
If you group by two or more columns, you may need to specify as_index=False or use Series.reset_index() to convert a MultiIndex Series to columns.
To aggregate string columns:
df1 = df.groupby('A')['B'].agg(list).reset_index()
For strings with a separator:
df2 = df.groupby('A')['B'].agg(','.join).reset_index()
Use GroupBy.size or GroupBy.count.
df1 = df.groupby('A').size().reset_index(name='COUNT')
Use GroupBy.transform.
df['C1'] = df.groupby('A')['C'].transform('sum')
The above is the detailed content of How Can I Perform Aggregation in Pandas?. For more information, please follow other related articles on the PHP Chinese website!