Categories
Python Answers

How to get statistics for each group using Python Pandas GroupBy?

Spread the love

To get statistics for each group using Python Pandas GroupBy, we can call the size method.

For instance, we write

df.groupby(['col1', 'col2']).size().reset_index(name='counts')

to call groupby with an array of columns.

Then we call size to get the row counts.

And then we call reset_index to return the values in a data a frame in the 'counts' column.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *