pandas 按列表查询行 [英] pandas query rows by list

查看:107
本文介绍了 pandas 按列表查询行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pandas数据框,想从数据框中返回与目标ID列表中出现的客户ID对应的行.

I have a pandas data frame and want to return the rows from the data frame corresponding to the customer ids that appear in a list of target ids.

例如,如果我的数据框如下所示:

For example, if my data frame looks like this:

id    Name    ...    ...
-------------------------
1     Bob     ...    ...
2     Dave    ...    ...
2     Dave    ...    ...
3     Phil    ...    ...
4     Rick    ...    ...
4     Rick    ...    ...

基本上,我想为在此数据框中出现多次的客户返回行.因此,我想返回所有出现多次的id.

Basically I want to return the rows for customers who appear more than once in this data frame. So I want to return all the ids that occur more than once.

id    Name    ...    ...
-------------------------
2     Dave    ...    ...
2     Dave    ...    ...
4     Rick    ...    ...
4     Rick    ...    ...

我可以通过执行以下操作获取ID列表

I can get a list of the ids by doing the following

grouped_ids = df.groupby('id').size()
id_list = grouped_ids[grouped_ids>1].index.tolist()

现在我想回到数据框并返回列表中与这些ID对应的所有行.

And now I'd like to go back to the data frame and return all the rows corresponding to those ids in the list.

这可能吗?

感谢您的帮助.

推荐答案

我想您正在寻找isin():

In [1]: import pandas as pd

In [2]: df = pd.DataFrame({'customer_id':range(5), 'A':('a', 'b', 'c', 'd', 'e')})

In [3]: df
Out[3]: 
   A  customer_id
0  a            0
1  b            1
2  c            2
3  d            3
4  e            4

In [4]: df[df.customer_id.isin((1,3))]
Out[4]: 
   A  customer_id
1  b            1
3  d            3

[edit]要匹配给定的目标列表,只需将其用作isin()方法的参数:

[edit] To match a given target list, just use it as argument for the isin() method:

In [5]: mylist = (1,3)

In [6]: df[df.customer_id.isin(mylist)]
Out[6]: 
       A  customer_id
1  abcde            1
3  abcde            3

这篇关于 pandas 按列表查询行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆