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