简体   繁体   English

在Pandas Python中子集DF

[英]Subset a DF in Pandas Python

I have a df : 我有一个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: 我想对仅选择列表中存在的cusips子集进行cusips

cusList = ('XXXX', 'ZZZZ')

The sub_df would be: sub_df将是:

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 : 您可以使用Series方法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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM