In [82]: d
Out[82]:
A B C D
0 John Doe 45 True False
1 Jane Smith 32 False False
2 Alan Holmes 55 False True
3 Eric Lamar 29 True True
解决方案1:
In [83]: d.loc[d.C | d.D]Out[83]: A B C D0 John Doe 45 True False2 Alan Holmes 55 False True3 Eric Lamar 29 True True
解决方案2:
In [94]: d[d[['C','D']].any(1)]Out[94]: A B C D0 John Doe 45 True False2 Alan Holmes 55 False True3 Eric Lamar 29 True True
解决方案3:
In [95]: d.query("C or D")Out[95]: A B C D0 John Doe 45 True False2 Alan Holmes 55 False True3 Eric Lamar 29 True TruePS:如果您将解决方案更改为:
df[(df['C']==True) | (df['D']==True)]
它也会工作
熊猫文档-布尔索引
[为什么我们不应该使用“ PEP投诉”
df["col_name"] is True代替df"col_name"] ==True?
In [11]: df = pd.Dataframe({"col":[True, True, True]})In [12]: dfOut[12]: col0 True1 True2 TrueIn [13]: df["col"] is TrueOut[13]: False # <----- oops, that's not exactly what we wanted


