In this tutorial, we're going to count values in Pandas DataFrame.

Check this article for most common values in DataFrame: Get most frequent values in Pandas DataFrame

Setup

Suppose we have a dataframe with data:

import pandas as pd
data = [('A',1, 0, 3, 1),
        ('A',1, 2, 5, 1),
        ('B',2, 1, 4, 3),
       ('B',3, 1, 0, 3),
       ('C',4, 3, 1, 2)]
cols = ('col_1', 'col_2', 'col_3', 'col_4', 'col_5' )
df = pd.DataFrame(data,
                 columns = cols)

data:

col_1 col_2 col_3 col_4 col_5
0 A 1 0 3 1
1 A 1 2 5 1
2 B 2 1 4 3
3 B 3 1 0 3
4 C 4 3 1 2

Step 1: Count values in Pandas Column

To count values in single Pandas column we can use method value_counts():

df['col_1'].value_counts()

The result is count of most frequent values sorted in ascending order:

A    2
B    2
C    1
Name: col_1, dtype: int64

Step 2: Count values in Multiple columns

Count values in multiple Pandas columns can be down with method .value_counts():

df[['col_1', 'col_2']].value_counts()

The result is is most common values per group/columns:

col_1  col_2
A      1        2
B      2        1
       3        1
C      4        1
dtype: int64

Step 3: Count values in Pandas DataFrame

To find the most common values in whole DataFrame we can combine:

  • melt
  • value_counts
df.melt()['value'].value_counts()

So we get the** count of all values for this DataFrame**:

1    7
3    5
2    3
A    2
B    2
4    2
0    2
C    1
5    1
Name: value, dtype: int64

Conclusion

In this short article we covered how to count values in Pandas DataFrame. We saw how to count values in single or multiple columns.

Finally with covered how to get most common values for the whole DataFrame