简体   繁体   English

使用列列表,用至少一个满足条件的列来过滤数据框?

[英]With a list of columns, filter a data frame with at least one of the columns meeting a condition?

Say that I have a dataframe with 50 columns. 假设我有一个包含50列的数据框。 Of these 50 columns, I have a list of 6 columns that are of interest. 在这50列中,我列出了感兴趣的6列。

list_cols = ['a', 'b', 'c', 'd', 'e', 'f']

I want to filter the dataframe such that at least one of these 6 cols must be <= 5. How would I go about doing this without having to tediously write something like: 我想过滤数据帧,以使这6个cols中至少有一个必须<= 5。

df.loc[(df['a'] <= 5) | 
       (df['b'] <= 5) |
       (df['c'] <= 5) |
       (df['d'] <= 5) |
       (df['e'] <= 5) |
       (df['f'] <= 5)]

Or writing a for loop on each column, concatenating, and dropping duplicate rows? 还是在每列上写一个for循环,连接并删除重复的行? Is there another option? 还有其他选择吗? Thanks. 谢谢。

只是使用any

df[df[list_cols].le(5).any(1)]

您也可以使用min

df[df[list_col].min(axis=1).le(5)]

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

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