2016-07-14 84 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