根据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