简体   繁体   English

计算 Pandas Python 中的过滤值

[英]Counting the filtered values in Pandas Python

col1  col2   col3
0     14     37
10    29     49
20    30     40

I want to know the number of filtered values only on single column.Such as how many numbers are greater than 45 in column 3. I tried我想知道仅在单个列上过滤值的数量。例如第 3 列中有多少个数字大于 45。我试过了

a = df["col3"] <= 45
a.sum()

But the out was 0. (This is an example of the actual data I am working on.My actual data has 10.000 rows and 100 columns)但输出为 0。(这是我正在处理的实际数据的一个示例。我的实际数据有 10.000 行和 100 列)

A simple way to do it would be:一个简单的方法是:

len(df[df['col3'] <= 45])
d = {"col1": [0, 10, 20], "col2": [14, 29, 30], "col3": [37, 49, 40]}
df = pd.DataFrame(d)
print(df)
df_filter = df[(df["col3"] <= 45)]
print("col3 sum\n",df_filter["col3"].sum()) # col3 data sum


print("---"*50)
print("all columns sum\n",df_filter.sum())# all data sum

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

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