pandas 使用多个 isin 子句的熊猫索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29757551/
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-09-13 23:13:59  来源:igfitidea点击:

pandas indexing using multiple isin clauses

pythonpandas

提问by Michael K

If I want to do is-in testing on multiple columns at once, I can do:

如果我想一次对多个列进行 is-in 测试,我可以这样做:

>>> from pandas import DataFrame
>>> df = DataFrame({'A': [1, 2, 3], 'B': [1, 4, 7], 'C' : [10, 12, 18]})   
>>> mask = df[['A','B']].isin({'A': [1, 3], 'B': [4, 7, 12]}).all(axis=1)
>>> df = df[mask]

That works--is there a more succinct solution?

那行得通——有没有更简洁的解决方案?

回答by Zero

You could put both the isinconditions in &

你可以把这两个isin条件都放在&

df[df['A'].isin([1, 3]) & df['B'].isin([4, 7, 12])]
   A  B   C
2  3  7  18

You could also use queryfunction like

您还可以使用query类似的功能

c_a = [1, 3]
c_b = [4, 7, 12]
df.query('(B in @c_b) & (A in @c_a)')

   A  B   C
2  3  7  18

回答by DSM

TBH, your current approach looks fine to me; I can't see a way with isinor filterto improve it, because I can't see how to get isinto use only the columns in the dictionary or filterto behave as an all.

TBH,你目前的方法对我来说很好;我看不到使用isinfilter改进它的方法,因为我看不到如何isin只使用字典中的列或filter作为all.

I don't like hardcoding column names, though, so I'd probably write this as

不过,我不喜欢对列名进行硬编码,所以我可能会将其写为

>>> keep = {'A': [1, 3], 'B': [4, 7, 12]}
>>> df[df[list(keep)].isin(keep).all(axis=1)]
   A  B   C
2  3  7  18

or with .locif I needed a handle.

或者.loc如果我需要一个把手。

回答by EdChum

You could put both conditions in as a mask and use &:

您可以将这两个条件作为掩码并使用&

In [12]:

df[(df['A'].isin([1,3])) & (df['B'].isin([4,7,12]))]
Out[12]:
   A  B   C
2  3  7  18

Here the conditions require parentheses ()around them due to operator precedence

()由于运算符优先级,这里的条件需要括号

Slightly more readable is to use query:

稍微更具可读性的是使用query

In [15]:

df.query('A in [1,3] and B in [4,7,12]')
Out[15]:
   A  B   C
2  3  7  18