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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.