将 pandas DataFrame query() 方法与 isin() 结合使用

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

Combine pandas DataFrame query() method with isin()

pythonpandasdataframe

提问by user4015990

So I want to use isin()method with df.query(), to select rows with idin a list: id_list. Similar questionwas asked before, but they used typical df[df['id'].isin(id_list)]method. I'm wondering if there is a way to use df.query()instead.

所以我想使用isin()方法 with df.query(),id在列表中选择行:id_list。之前也有人问过类似的问题,但他们使用的是典型的df[df['id'].isin(id_list)]方法。我想知道是否有一种方法可以df.query()代替。

df = pd.DataFrame({'a': list('aabbccddeeff'), 'b': list('aaaabbbbcccc'),
                   'c': np.random.randint(5, size=12),
                   'd': np.random.randint(9, size=12)})

id_list = ["a", "b", "c"]

And this yields an error

这会产生错误

df.query('a == id_list')

回答by Seiji Armstrong

You can also include the list within the query string:

您还可以在查询字符串中包含该列表:

>>> df.query('a in ["a", "b", "c"]')

This is the same as:

这与:

>>> df.query('a in @id_list')

回答by maxymoo

From the docsfor query

文档query

You can refer to variables in the environment by prefixing them with an '@' character like @a + b.

您可以通过在变量前加上“@”字符来引用环境中的变量,例如 @a + b

In your case:

在你的情况下:

In [38]: df.query('a == @id_list')
Out[38]:
   a  b  c  d
0  a  a  3  4
1  a  a  4  5
2  b  a  2  3
3  b  a  1  5
4  c  b  2  4
5  c  b  1  2

回答by Alexander

This appears to work:

这似乎有效:

>>> df.query('a == {0}'.format(id_list))
   a  b  c  d
0  a  a  4  1
1  a  a  0  7
2  b  a  2  1
3  b  a  0  1
4  c  b  4  0
5  c  b  4  2

Whether or not it is more clear is a matter of personal taste.

是否更清楚是个人品味的问题。