Reputation: 79
I currently have DataFrame with 50 columns and around 50000 rows. I'm trying to find the total amount of times a value (e.g. 2) appears in the entire DataFrame.
The DataFrame only contains values between 0 to 7. I am able to execute the code for a single column using this:
print(df['col1'].value_counts())
I then attempted to create a for loop like the one shown below:
for cols in df:
print(df[cols].value_counts())
This works, but it prints it out as individual results for each column.
Instead of having the results split up per column, I'm trying to get something like what's shown below, but for all the columns in the DataFrame combined and not just 1 column.
val no.
7.0 165
3.0 127
5.0 118
6.0 112
2.0 98
4.0 88
1.0 64
0.0 21
Name: col1, dtype: int64
Any help would be greatly appreciated!
Upvotes: 5
Views: 4778
Reputation: 18218
You can also try using Counter
:
from collections import Counter
print(pd.DataFrame(Counter(df.values.flatten()), index=['Count']).T)
Upvotes: 4
Reputation: 323376
You may need check with 1st stack
then value_counts
and now you can select what you need from index
df.stack().value_counts()
Upvotes: 6
Reputation: 15206
Either for a specific value:
(df.values == 2).sum()
or for all:
np.unique(df.values, return_counts=True)
Upvotes: 8