Python 在 Pandas DataFrame 上选择具有条件的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37663931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:45:57  来源:igfitidea点击:

Selecting columns with condition on Pandas DataFrame

pythonpandasdataframeconditional-statements

提问by user3368526

I have a dataframe looking like this.

我有一个看起来像这样的数据框。

    col1    col2
0   something1  something1
1   something2  something3
2   something1  something1
3   something2  something3
4   something1  something2  

I'm trying to filter all rows that have something1either on col1or col2. If I just need the condition logic on a column, I can do it with df[df.col1 == 'something1']but would there be a way to do it with multiple columns?

我正在尝试过滤所有具有something1oncol1或 的行col2。如果我只需要列上的条件逻辑,我可以用它来做,df[df.col1 == 'something1']但有没有办法用多列来做?

采纳答案by jezrael

You can use allwith boolean indexing:

你可以用allboolean indexing

print ((df == 'something1').all(1))
0     True
1    False
2     True
3    False
4    False
dtype: bool

print (df[(df == 'something1').all(1)])
         col1        col2
0  something1  something1
2  something1  something1

EDIT:

编辑:

If need select only some columns you can use isinwith boolean indexingfor selecting desired columnsand then use subset- df[cols]:

如果需要选择某些列,您可以使用isinboolean indexing用于选择所需columns,然后使用subset- df[cols]

print (df)
         col1        col2 col3
0  something1  something1    a
1  something2  something3    s
2  something1  something1    r
3  something2  something3    a
4  something1  something2    a

cols = df.columns[df.columns.isin(['col1','col2'])]
print (cols)
Index(['col1', 'col2'], dtype='object')

print (df[(df[cols] == 'something1').all(1)])
         col1        col2 col3
0  something1  something1    a
2  something1  something1    r

回答by Naomi Fridman

Why not:

为什么不:

df[(df.col1 == 'something1') | (df.col2 == 'something1')]

outputs:

输出:

    col1    col2
0   something1  something1
2   something1  something1
4   something1  something2

回答by Sincole Brans

To apply one condition to the whole dataframe

将一个条件应用于整个数据框

df[(df == 'something1').any(axis=1)]