本文翻译自: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. 会匹配PANDAS , PanDAs , paNdAs123等。
#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')
本文介绍如何使用Python的Pandas库进行高级数据筛选操作,包括使用isin方法和str.contains方法过滤数据帧,以及如何利用正则表达式进行部分匹配和子字符串查找,同时提供了使用query方法进行直接查询的示例。
10万+

被折叠的 条评论
为什么被折叠?



