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')
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3