将pandas DataFrame query()方法与isin()结合使用 [英] Combine pandas DataFrame query() method with isin()
本文介绍了将pandas DataFrame query()方法与isin()结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,我想对df.query()
使用isin()
方法,以在列表中选择id
的行:id_list
.之前曾问过类似的问题,但是他们使用了典型的df[df['id'].isin(id_list)]
方法.我想知道是否可以使用df.query()
代替.
So I want to use isin()
method with df.query()
, to select rows with id
in a list: id_list
. Similar question was 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.
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"]
这会产生错误
df.query('a == id_list')
推荐答案
查看全文