pandas 过滤某一列的值,根据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.

I tried with this code:

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".

For example:

MachineName Logs Jobs Performance

121 Yes No Yes

122 Yes Yes Yes

123 Yes No No

125 Yes Yes Yes

126 No No No

Output:

MachineName Logs Jobs Performance

121 Yes No Yes

123 Yes No No

126 No No No

解决方案

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]

The resulting output is as expected:

MachineName Logs Jobs Performance

0 121 Yes No Yes

2 123 Yes No No

4 126 No No No

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值