简体   繁体   English

计算整个DataFrame中值的数量

[英]Count number of values in an entire DataFrame

I currently have DataFrame with 50 columns and around 50000 rows. 我目前有50列和大约50000行的DataFrame。 I'm trying to find the total amount of times a value (eg 2) appears in the entire DataFrame. 我试图找到一个值(例如2)出现在整个DataFrame中的总次数。

The DataFrame only contains values between 0 to 7. I am able to execute the code for a single column using this: DataFrame仅包含0到7之间的值。我可以使用以下命令为单个列执行代码:

print(df['col1'].value_counts())

I then attempted to create a for loop like the one shown below: 然后,我尝试创建一个for循环,如下所示:

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. 我没有使结果按列划分,而是尝试获得类似于以下所示的内容,但对于DataFrame中的所有列进行合并,而不仅仅是1列。

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 : 您也可以尝试使用Counter

from collections import Counter

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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