2016-07-14 83 views
1

我有一个数据帧(DF)非旋转数据框列筛选数据透视表

    id      company   sector currency   price 
0  BBG.MTAA.MS.S     MEDIASET SPA Communications  EUR  4.334000 
1 BBG.MTAA.TIT.S   TELECOM ITALIA SPA Communications  EUR  1.091000  
2 BBG.XETR.DTE.S  DEUTSCHE TELEKOM AG-REG Communications  EUR  15.460000 
3 BBG.XLON.BARC.S     BARCLAYS PLC  Financial  GBp  3.414498  
4 BBG.XLON.BTA.S     BT GROUP PLC Communications  GBp  5.749122  
5 BBG.XLON.HSBA.S    HSBC HOLDINGS PLC  Financial  GBp  6.716041  
6 BBG.XLON.LLOY.S  LLOYDS BANKING GROUP PLC  Financial  GBp  1.027752  
7 BBG.XLON.STAN.S  STANDARD CHARTERED PLC  Financial  GBp  9.707300  
8 BBG.XLON.TRIL.S  THOMSON REUTERS UK LTD Communications  GBp    NaN   
9 BBG.XLON.VOD.S   VODAFONE GROUP PLC Communications  GBp  3.035487  
10 BBG.XMCE.BBVA.S BANCO BILBAO VIZCAYA ARGENTA  Financial  EUR  7.866000 

我可以在行业领域创建一个数据透视表(以找出有多少公司使用的是同一个部门下面的代码:

sectorPivot = df.pivot_table(index=['sector'], aggfunc='count') 

,看起来像这样:

   currency id  company 
sector        
Communications   6 6   6 
Financial    5 5   5 

不过,我想筛选出样稿anies,其价格等于“的NaN”所以我有一个枢轴表看起来像

   currency id  company 
sector        
Communications   5 5   5 
Financial    5 5   5 

(请注意,通信部门的计数已经减少1 6至5由于'NaN'价格对于broad_sector股

可能有人让我知道我是如何做到这一点,请之一?

非常感谢

+0

你不能叫'dropna'第一? 'sectorPivot = df.dropna()。pivot_table(index = ['sector'],aggfunc ='count')'应该可以工作。或者特别指定'df [df ['price']。notnull()]。pivot_table(index = ['sector'],aggfunc ='count')' – EdChum

回答

1

使用dropna(subset=['price']您枢纽提前。

df.dropna(subset=['price']).pivot_table(index=['sector'], aggfunc='count') 

enter image description here