根据Pandas数据框中的特定值过滤所有行 [英] Filtering all rows based on a specific value in Pandas dataframe

查看:878
本文介绍了根据Pandas数据框中的特定值过滤所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当条件满足时,我有一个数据帧为是,否则为否。现在,我想检索其中有否的所有行。

I have a dataframe that has "yes" when a condition is satisfied and "no" when it is not. Now, I would like to retrieve all the rows that has "No" in it.

我尝试使用这段代码:

 df2 = df[df['Logs'].astype(str).str.contains('No')] 
 df3 = df[df['Jobs'].astype(str).str.contains('No')] 
 df4 = df[df['Performance'].astype(str).str.contains('No')] 
 df5 = df2 | df3 | df4

我收到错误不支持的操作数类型。

I got the error "unsupported operand types".

例如:

 MachineName    Logs   Jobs   Performance
 121            Yes    No      Yes
 122            Yes    Yes     Yes
 123            Yes    No      No
 125            Yes    Yes     Yes
 126            No     No      No

输出:

 MachineName    Logs   Jobs   Performance
 121            Yes    No      Yes
 123            Yes    No      No
 126            No     No      No


推荐答案

所有列的平等检查'否',然后使用 any 以获取一个布尔数组。

Do an equality check on all columns you want to be 'No', and then use any to get a Boolean array.

condition = (df[['Logs', 'Jobs', 'Performance']] == 'No').any(axis=1)
df2 = df[condition]

结果输出如预期:

   MachineName Logs Jobs Performance
0          121  Yes   No         Yes
2          123  Yes   No          No
4          126   No   No          No

这篇关于根据Pandas数据框中的特定值过滤所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆