"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How Can I Perform Aggregation in Pandas?

How Can I Perform Aggregation in Pandas?

Published on 2024-12-21
Browse:323

How Can I Perform Aggregation in Pandas?

Aggregation in Pandas

How can I perform aggregation with Pandas?

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()

No DataFrame after aggregation! What happened?

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.

How can I aggregate mainly strings columns (to lists, tuples, strings with separator)?

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()

How can I aggregate counts?

Use GroupBy.size or GroupBy.count.

df1 = df.groupby('A').size().reset_index(name='COUNT')

How can I create a new column filled by aggregated values?

Use GroupBy.transform.

df['C1'] = df.groupby('A')['C'].transform('sum')
Latest tutorial More>

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