当行包含某些文本时计算行数

编程入门 行业动态 更新时间:2024-10-25 02:22:14
本文介绍了当行包含某些文本时计算行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能是一个简单的问题,但我找不到简单的答案.例如,让我们在数据框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

更多推荐

当行包含某些文本时计算行数

本文发布于:2023-10-24 09:35:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523589.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时计   行数   文本

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!