简体   繁体   English

计算熊猫数据框列中满足条件的单元格数

[英]Count number of cells satisfying a condition in pandas dataframe column

Hi I have following dataframe 嗨,我有以下数据框

Name Gender Age
ABC  M      21
C-DF F      24

I have to count the number of cells having a dash in them. 我必须计算其中带有破折号的单元格的数量。 I used the regular expression and wrote the code written below 我使用了正则表达式并编写了下面的代码

df[df['Name'] == r'-+'].sum()

The output that I am getting is. 我得到的输出是。 I don't understand what mistake did I do or if I wrote the wrong command. 我不明白我做了什么错误,或者我写错了命令。

Name             0.0
Gender           0.0
Age              0.0
dtype: float64

Please help. 请帮忙。 Thanks. 谢谢。

Use contains for boolean mask with sum - True s are processes like 1 s: 使用contains与布尔面具sum - True s为状突起1 S:

print (df)
   Name Gender  Age
0  AB+C      M   21
1  C-DF      F   24

a = df['Name'].str.contains(r'-').sum()
print (a)
1

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

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