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!
Either for a specific value:
(df.values == 2).sum()
or for all:
np.unique(df.values, return_counts=True)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With