当行包含某些文本时计算行数 [英] Count number of rows when row contains certain text
本文介绍了当行包含某些文本时计算行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能是一个简单的问题,但我找不到简单的答案.例如,让我们在数据框df1中获取以下列状态:
Probably a simple question but I could not find a simple answer. Let's for example take the following column Status within a dataframe df1:
**Status**
Planned
Unplanned
Missing
Corrected
当一个单元格包含计划的"和缺少的"时,我想对行进行计数.我尝试了以下方法:
I would like to count the rows when a cell contains, Planned and Missing. I tried the following:
test1 = df1['Status'].str.contains('Planned|Missing').value_counts()
状态"列来自以下类型:对象.我的代码行有什么问题?
The column Status is from the type: object. What's wrong with my line of code?
推荐答案
您可以使用布尔条件过滤df,然后调用len
:
You can just filter the df with your boolean condition and then call len
:
In [155]:
len(df[df['Status'].str.contains('Planned|Missing')])
Out[155]:
2
或在您的value_counts
中使用索引True
:
In [158]:
df['Status'].str.contains('Planned|Missing').value_counts()[True]
Out[158]:
2
这篇关于当行包含某些文本时计算行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文