如果列中的值位于值的集合列表[重复]中,则筛选数据行我有一只Python熊猫DataFramerpt: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']<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']..这意味着我需要这样的语法:stk_list = ['600809','600141','600329']rst = rpt[rpt['STK_ID'] in stk_list] # this does not works in pandas既然熊猫不接受上面的命令,如何达到目标?
3 回答
料青山看我应如是
TA贡献1772条经验 获得超8个赞
isin()
str.contains
'600'
>>> 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' ...... ... ...
'STK_ID'
endstrings = ['01$', '02$', '05$']
|
str.contains
>>> rpt[rpt['STK_ID'].str.contains('|'.join(endstrings)]... STK_ID ...... '155905' ...... '633101' ...... '210302' ...... ... ...
contains
case=False
str.contains('pandas', case=False)
PANDAS
, PanDAs
, paNdAs123
添加回答
举报
0/150
提交
取消