[英]Pandas dataframe check if a value exists in multiple columns for one row
I want to print out the row where the value is "True" for more than one column.我想为多列打印出值为“True”的行。
For example if data frame is the following:例如,如果数据框如下:
Remove Ignore Repair
0 True False False
1 False True True
2 False True False
I want it to print:我希望它打印:
1
Is there an elegant way to do this instead of bunch of if statements?有没有一种优雅的方法来代替一堆 if 语句?
import pandas as pd
df = pd.DataFrame({'a':[False, True, False],'b':[False, True, False], 'c':[True, False, False,]})
print(df)
print("Ans: ",df[(df.sum(axis=1)>1)].index.tolist())
output: output:
a b c
0 False False True
1 True True False
2 False False False
Ans: [1]
You can just sum booleans as they will be interpreted as True=1, False=0:您可以对布尔值求和,因为它们将被解释为 True=1,False=0:
df.sum(axis=1) > 1
So to filter to rows where this evaluates as True:因此,要过滤到评估为 True 的行:
df.loc[df.sum(axis=1) > 1]
Or the same thing but being more explicit about converting the booleans to integers:或者同样的事情,但更明确地将布尔值转换为整数:
df.loc[df.astype(int).sum(axis=1) > 1]
To get the first row that meets the criteria:获取满足条件的第一行:
df.index[df.sum(axis=1).gt(1)][0]
Output: Output:
Out[14]: 1
Since you can get multiple matches, you can exclude the [0]
to get all the rows that meet your criteria由于您可以获得多个匹配项,因此您可以排除
[0]
以获取满足您条件的所有行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.