Categories
Python Answers

How to groupby value counts on the dataframe with Python Pandas?

Spread the love

Sometimes, we want to groupby value counts on the dataframe with Python Pandas.

In this article, we’ll look at how to groupby value counts on the dataframe with Python Pandas.

How to groupby value counts on the dataframe with Python Pandas?

To groupby value counts on the dataframe with Python Pandas, we call groupby with size.

For instance, we write

df.groupby(['id', 'group', 'term']).size().unstack(fill_value=0)

to call groupby with a list of columns we want to group by.

And then we call size to get the counts of each group.

Conclusion

To groupby value counts on the dataframe with Python Pandas, we call groupby with size.

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 *