Pandas:用百分比制作数据透视表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40301973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:17:57  来源:igfitidea点击:

Pandas: make pivot table with percentage

pythonpandasuniquepivot-tablepercentage

提问by Petr Petrov

I have dataframe

我有数据框

ID,url,used_at,active_seconds,domain
61a77f9e5fd52a50c10cd2d4d886ec68,mazdaspb.ru,2015-01,6,mazdaspb.ru
61a77f9e5fd52a50c10cd2d4d886ec68,mazdaspb.ru/cars/mazda-cx-5/crossover/overview,2015-01,12,mazdaspb.ru
61a77f9e5fd52a50c10cd2d4d886ec68,mazdaspb.ru/cars/mazda-cx-5/crossover/overview,2015-01,19,mazdaspb.ru
61a77f9e5fd52a50c10cd2d4d886ec68,vw-stat.ru,2015-01,40,vw-stat.ru
61a77f9e5fd52a50c10cd2d4d886ec68,vw-stat.ru/models/new_tiguan,2015-01,12,vw-stat.ru
61a77f9e5fd52a50c10cd2d4d886ec68,vw-stat.ru/models/new_tiguan/comps,2015-01,48,vw-stat.ru
61a77f9e5fd52a50c10cd2d4d886ec68,vw-stat.ru/models/new_tiguan/comps/new_tiguan_track_field,2015-01,4,vw-stat.ru
61a77f9e5fd52a50c10cd2d4d886ec68,vw-stat.ru/models/new_tiguan/comps/new_tiguan_track_field?engine_type=DIESEL&DIESEL=engines_4e53a3c8e986d,2015-01,78,vw-stat.ru
41c2fd7a372729dfe336e44730169f28,avito.ru/saratov/avtomobili_s_probegom/volkswagen/golf?f=188_886b887,2015-01,8,avito.ru
41c2fd7a372729dfe336e44730169f28,avito.ru/saratov/avtomobili_s_probegom/volkswagen/golf?f=188_886b887,2015-01,1,avito.ru
41c2fd7a372729dfe336e44730169f28,avito.ru/saratov/avtomobili_s_probegom/volkswagen/golf?f=188_886b887,2015-01,2,avito.ru

I need to get to make pivot table, and there are should be values of percentage of all unique ID. I can get

我需要制作数据透视表,并且应该有所有唯一 ID 的百分比值。我可以得到

group = pd.pivot_table(df, index='used_at', columns='domain', values='ID', aggfunc=(lambda x: x.count()))

but it return quantity of unique ID to every domain to every month. How can I convert that to percentage?

但它每个月都会向每个域返回唯一 ID 的数量。如何将其转换为百分比?

回答by jezrael

IIUC you can use parameter marginsfor sum values in pivot_tableand then divide all values last row Allby div:

IIUC 您可以将参数margins用于总和值pivot_table,然后将最后一行All的所有值除以div

group = pd.pivot_table(df, 
                       index='used_at', 
                       columns='domain', 
                       values='ID', 
                       aggfunc=len, 
                       margins=True)
print (group)
domain   avito.ru  mazdaspb.ru  vw-stat.ru   All
used_at                                         
2015-01       3.0          3.0         5.0  11.0
All           3.0          3.0         5.0  11.0

print (group.iloc[:-1])
domain   avito.ru  mazdaspb.ru  vw-stat.ru   All
used_at                                         
2015-01       3.0          3.0         5.0  11.0

print (group.iloc[-1])
domain
avito.ru        3.0
mazdaspb.ru     3.0
vw-stat.ru      5.0
All            11.0
Name: All, dtype: float64

print (group.iloc[:-1].div(group.iloc[-1], axis=1) * 100)
domain   avito.ru  mazdaspb.ru  vw-stat.ru    All
used_at                                          
2015-01     100.0        100.0       100.0  100.0

Solution with divide by individual count with divand mul:

解决方案通过与个人计分divmul

group = pd.pivot_table(df, 
                       index='used_at',
                       columns='domain', 
                       values='ID', 
                       aggfunc=len)
          .div(len(df.index))
          .mul(100)
print (group)

domain    avito.ru  mazdaspb.ru  vw-stat.ru
used_at                                    
2015-01  27.272727    27.272727   45.454545

回答by Nickil Maveli

Divide the individual count values obtained with the total number of rows of the DFto get it's percentage distribution as shown:

将获得的单个计数值与 的总行数相除,DF以获得其百分比分布,如下所示:

func = lambda x: 100*x.count()/df.shape[0]
pd.pivot_table(df, index='used_at', columns='domain', values='ID', aggfunc=func)

Image

图片