简体   繁体   English

按非透视数据框列过滤透视表

[英]Filtering a pivot table by non pivot dataframe column

I have a dataframe (df)我有一个数据框(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

I can create a pivot table on the sector field (to find out how many companies are in the same sector) using the following code:我可以使用以下代码在部门字段上创建数据透视表(以找出同一部门中有多少家公司):

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

Which looks like this:看起来像这样:

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

However I would like to filter out the companies with a price that is equal to 'NaN' so I have a pivot table that looks like但是我想过滤掉价格等于“NaN”的公司,所以我有一个看起来像的数据透视表

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

(Note that the count of the communications sector has decreased by 1 from 6 to 5 due to the 'NaN' price for one of the broad_sector stocks). (请注意,由于其中一只大板块股票的'NaN'价格,通信板块的数量从 6 减少了 1 到 5)。

How can I do this?我该怎么做?

Use dropna(subset=['price'] ahead of your pivot.在您的枢轴之前使用dropna(subset=['price']

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

在此处输入图片说明

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

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