如果列中的值在一组值的列表中,则过滤数据框行 [英] Filter dataframe rows if value in column is in a set list of values

查看:76
本文介绍了如果列中的值在一组值的列表中,则过滤数据框行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python pandas DataFrame rpt:

I have a Python pandas DataFrame rpt:

rpt
<class 'pandas.core.frame.DataFrame'>
MultiIndex: 47518 entries, ('000002', '20120331') to ('603366', '20091231')
Data columns:
STK_ID                    47518  non-null values
STK_Name                  47518  non-null values
RPT_Date                  47518  non-null values
sales                     47518  non-null values

我可以过滤库存ID为'600809'的行,如下所示:rpt[rpt['STK_ID'] == '600809']

I can filter the rows whose stock id is '600809' like this: rpt[rpt['STK_ID'] == '600809']

<class 'pandas.core.frame.DataFrame'>
MultiIndex: 25 entries, ('600809', '20120331') to ('600809', '20060331')
Data columns:
STK_ID                    25  non-null values
STK_Name                  25  non-null values
RPT_Date                  25  non-null values
sales                     25  non-null values

,我想将一些股票的所有行放在一起,例如['600809','600141','600329'].这意味着我想要这样的语法:

and I want to get all the rows of some stocks together, such as ['600809','600141','600329']. That means I want a syntax like this:

stk_list = ['600809','600141','600329']

rst = rpt[rpt['STK_ID'] in stk_list] # this does not works in pandas 

由于熊猫不接受上述命令,如何实现目标?

Since pandas not accept above command, how to achieve the target?

推荐答案

使用isin方法:

rpt[rpt['STK_ID'].isin(stk_list)]

这篇关于如果列中的值在一组值的列表中,则过滤数据框行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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