简体   繁体   English

生成在pandas.DataFrame中选择行的条件

[英]Generate condition for selecting rows in pandas.DataFrame

For the dataframe df, I am selecting the rows that have True values either in column 'a' or 'b'. 对于数据帧df,我在“a”或“b”列中选择具有True值的行。

>>> df
Out[127]: 
       a      b
0  False  False
1   True   True
2   True  False
>>> con = (df['a'] == True) | (df['b'] == True)
>>> con
Out[129]: 
0    False
1     True
2     True
dtype: bool
>>> df[con]
Out[130]: 
      a      b
1  True   True
2  True  False

There are only two columns in the dataframe. 数据框中只有两列。 For the actual code, the number of such columns is a variable. 对于实际代码,此类列的数量是变量。 How can the condition con be generated on-the-fly? 如何能在条件con生成上最靠谱?

Say, when df has 26 columns from a through z , I want something like 比方说,当df从az有26列时,我想要类似的东西

>>> con = (df['a'] == True) | (df['b'] == True) | ... (df['y'] == True) | (df['z'] == True)

which I can use to get the desired rows 我可以使用它来获得所需的行

You can use DataFrame.any : 您可以使用DataFrame.any

>>> df = pd.DataFrame(np.random.choice([True]+[False]*5, size=(6,5)), columns=list("abcde"))
>>> df
       a      b      c      d      e
0  False  False  False  False  False
1  False  False   True  False  False
2  False  False   True  False  False
3  False  False  False  False  False
4  False  False  False  False   True
5  False  False  False  False  False
>>> df.any(axis=1)
0    False
1     True
2     True
3    False
4     True
5    False
dtype: bool
>>> df[df.any(axis=1)]
       a      b      c      d      e
1  False  False   True  False  False
2  False  False   True  False  False
4  False  False  False  False   True

And as always you can use df.loc[df.any(axis=1)] if you want to ensure you have a handle on the original. 和往常一样,如果你想确保你有一个原始句柄,你可以使用df.loc[df.any(axis=1)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM