简体   繁体   中英

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 (eg 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)

您可能需要先检查第一个stack然后检查value_counts ,现在您可以从索引中选择所需的内容

df.stack().value_counts()

You can also try using Counter :

from collections import Counter

print(pd.DataFrame(Counter(df.values.flatten()), index=['Count']).T)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM