如何通过“内”/“内”过滤“大 pandas ”的DataFrame行? [英] How to filter the DataFrame rows of pandas by "within"/"in"?

查看:112
本文介绍了如何通过“内”/“内”过滤“大 pandas ”的DataFrame行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python大熊猫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

我可以过滤这些库存编号为'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


$ b $我想把一些股票的所有行都整合在一起,例如 ['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 


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

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

推荐答案

使用 isin 方法。 rpt [rpt ['STK_ID']。isin(stk_list)]

这篇关于如何通过“内”/“内”过滤“大 pandas ”的DataFrame行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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