Menu Close

How to count the frequency that a value occurs in a Python Pandas dataframe column?

To count the frequency that a value occurs in a Python Pandas dataframe column, we call groupby and count.

For instance, we write

df = pd.DataFrame({'a':list('abssbab')})
df.groupby('a').count()

to create the df data frame.

Then we call groupby with 'a' to group the 'a' column values into groups.

Then we call count to count the number of values in each group.

Posted in Python, Python Answers