Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter dataframe rows if value in column is in a set list of values [duplicate]

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 

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 

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?

like image 994
bigbug Avatar asked Aug 22 '12 03:08

bigbug


People also ask

How do you check if there are duplicate rows in pandas DataFrame?

The pandas. DataFrame. duplicated() method is used to find duplicate rows in a DataFrame. It returns a boolean series which identifies whether a row is duplicate or unique.

How do you drop duplicate rows in pandas based on a column?

Use DataFrame. drop_duplicates() to Drop Duplicate and Keep First Rows. You can use DataFrame. drop_duplicates() without any arguments to drop rows with the same values on all columns.


1 Answers

Use the isin method:

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

like image 60
BrenBarn Avatar answered Oct 31 '22 10:10

BrenBarn