如果列中的值在一组值的列表中,则过滤数据帧行[重复]

本文翻译自:Filter dataframe rows if value in column is in a set list of values [duplicate]

This question already has an answer here: 这个问题已经在这里有了答案:

I have a Python pandas DataFrame rpt : 我有一个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

I can filter the rows whose stock id is '600809' like this: rpt[rpt['STK_ID'] == '600809'] 我可以像这样过滤库存ID为'600809'的行: 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

and I want to get all the rows of some stocks together, such as ['600809','600141','600329'] . 我想将所有股票的所有行放在一起,例如['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? 由于大熊猫不接受上述命令,如何实现目标?


#1楼

参考:https://stackoom.com/question/oct3/如果列中的值在一组值的列表中-则过滤数据帧行-重复


#2楼

Use the isin method. 使用isin方法。 rpt[rpt['STK_ID'].isin(stk_list)] . rpt[rpt['STK_ID'].isin(stk_list)]


#3楼

您还可以通过以下方式使用范围:

b = df[(df['a'] > 1) & (df['a'] < 5)]

#4楼

isin() is ideal if you have a list of exact matches, but if you have a list of partial matches or substrings to look for, you can filter using the str.contains method and regular expressions. 如果您有一个完全匹配的列表,则isin()是理想的选择,但是如果要查找部分匹配或子字符串的列表,则可以使用str.contains方法和正则表达式进行过滤。

For example, if we want to return a DataFrame where all of the stock IDs which begin with '600' and then are followed by any three digits: 例如,如果我们要返回一个DataFrame,其中所有以'600'开头,然后再跟任意三位数字的股票ID:

>>> rpt[rpt['STK_ID'].str.contains(r'^600[0-9]{3}$')] # ^ means start of string
...   STK_ID   ...                                    # [0-9]{3} means any three digits
...  '600809'  ...                                    # $ means end of string
...  '600141'  ...
...  '600329'  ...
...      ...   ...

Suppose now we have a list of strings which we want the values in 'STK_ID' to end with, eg 假设现在有一个字符串列表,我们希望以'STK_ID'的值结尾,例如

endstrings = ['01$', '02$', '05$']

We can join these strings with the regex 'or' character | 我们可以将这些字符串与正则表达式“或”字符连接起来| and pass the string to str.contains to filter the DataFrame: 并将字符串传递给str.contains以过滤DataFrame:

>>> rpt[rpt['STK_ID'].str.contains('|'.join(endstrings)]
...   STK_ID   ...
...  '155905'  ...
...  '633101'  ...
...  '210302'  ...
...      ...   ...

Finally, contains can ignore case (by setting case=False ), allowing you to be more general when specifying the strings you want to match. 最后, contains可以忽略大小写(通过设置case=False ),使您在指定要匹配的字符串时更加通用。

For example, 例如,

str.contains('pandas', case=False)

would match PANDAS , PanDAs , paNdAs123 , and so on. 会匹配PANDASPanDAspaNdAs123等。


#5楼

You can also directly query your DataFrame for this information. 您也可以直接在DataFrame中查询此信息。

rpt.query('STK_ID in (600809,600141,600329)')

Or similarly search for ranges: 或类似地搜索范围:

rpt.query('60000 < STK_ID < 70000')

#6楼

您可以使用query ,即:

b = df.query('a > 1 & a < 5')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值