将 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
Combine pandas DataFrame query() method with isin()
提问by user4015990
So I want to use isin()
method with df.query()
, to select rows with id
in 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
回答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.
是否更清楚是个人品味的问题。