Menu Close

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

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.

Posted in Python, Python Answers