I have a df
:
date cusip value
2012-12-20 XXXX 4.23
2012-12-20 YYYY 6.34
2012-12-20 ZZZZ 8.12
2012-12-21 XXXX 5.78
2012-12-21 YYYY 6.62
2012-12-21 ZZZZ 9.09
I want to subset where I select only the cusips
that exist in a list:
cusList = ('XXXX', 'ZZZZ')
The sub_df
would be:
date cusip value
2012-12-20 XXXX 4.23
2012-12-20 ZZZZ 8.12
2012-12-21 XXXX 5.78
2012-12-21 ZZZZ 9.09
Any recommendations? Thanks.
You can use the Series method isin
:
In [1]: df = pd.read_csv(cusp.csv, sep='\s+')
In [2]: df.cusip.isin(['XXXX', 'ZZZZ'])
Out[2]:
0 True
1 False
2 True
3 True
4 False
5 True
Name: cusip
In [3]: df[df.cusip.isin(['XXXX', 'ZZZZ'])]
Out[3]:
date cusip value
0 2012-12-20 XXXX 4.23
2 2012-12-20 ZZZZ 8.12
3 2012-12-21 XXXX 5.78
5 2012-12-21 ZZZZ 9.09
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.