2

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.

1
  • I tried isin but wanted to make sure there was not another way about it. Thanks. Commented Jan 14, 2013 at 17:27

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.