How to get value counts for multiple columns at once in Pandas DataFrame?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to get value counts for multiple columns at once in Pandas DataFrame in Python. So Here I am Explain to you all the possible Methods here.

How to get value counts for multiple columns at once in Pandas DataFrame?

1. How to get value counts for multiple columns at once in Pandas DataFrame?

There is actually a fairly interesting and advanced way of doing this problem with crosstab and melt

2. value counts for multiple columns at once in Pandas DataFrame

There is actually a fairly interesting and advanced way of doing this problem with crosstab and melt

Method 1

Just call apply and pass pd.Series.value_counts:

In [212]:
df = pd.DataFrame(np.random.randint(0, 2, (10, 4)), columns=list('abcd'))
df.apply(pd.Series.value_counts)
Out[212]:
a  b  c  d
0  4  6  4  3
1  6  4  6  7

Method 2

There is actually a fairly interesting and advanced way of doing this problem with crosstab and melt

df = pd.DataFrame({'a': ['table', 'chair', 'chair', 'lamp', 'bed'],
'b': ['lamp', 'candle', 'chair', 'lamp', 'bed'],
'c': ['mirror', 'mirror', 'mirror', 'mirror', 'mirror']})

df

a       b       c
0  table    lamp  mirror
1  chair  candle  mirror
2  chair   chair  mirror
3   lamp    lamp  mirror
4    bed     bed  mirror

We can first melt the DataFrame

df1 = df.melt(var_name='columns', value_name='index')
df1

columns   index
0        a   table
1        a   chair
2        a   chair
3        a    lamp
4        a     bed
5        b    lamp
6        b  candle
7        b   chair
8        b    lamp
9        b     bed
10       c  mirror
11       c  mirror
12       c  mirror
13       c  mirror
14       c  mirror

And then use the crosstab function to count the values for each column. This preserves the data type as ints which wouldn’t be the case for the currently selected answer:

pd.crosstab(index=df1['index'], columns=df1['columns'])

columns  a  b  c
index
bed      1  1  0
candle   0  1  0
chair    2  1  0
lamp     1  2  0
mirror   0  0  5
table    1  0  0

Or in one line, which expands the column names to parameter names with ** (this is advanced)

pd.crosstab(**df.melt(var_name='columns', value_name='index'))

Also, value_counts is now a top-level function. So you can simplify the currently selected answer to the following:

df.apply(pd.value_counts)

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.