Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of values in an entire DataFrame

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!

like image 824
577e94982d620b84f7c536d5e76f1e Avatar asked Dec 11 '22 04:12

577e94982d620b84f7c536d5e76f1e


1 Answers

Either for a specific value:

(df.values == 2).sum()

or for all:

np.unique(df.values, return_counts=True)
like image 149
Julien Avatar answered Dec 21 '22 16:12

Julien