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

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.