Pandas:用百分比制作数据透视表 [英] Pandas: make pivot table with percentage
本文介绍了Pandas:用百分比制作数据透视表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有数据框
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
我需要制作数据透视表,并且应该有所有唯一 ID 的百分比值.我可以得到
I need to get to make pivot table, and there are should be values of percentage of all unique ID. I can get
group = pd.pivot_table(df, index='used_at', columns='domain', values='ID', aggfunc=(lambda x: x.count()))
但它每月返回每个域的唯一 ID 数量.如何将其转换为百分比?
but it return quantity of unique ID to every domain to every month. How can I convert that to percentage?
推荐答案
IIUC you can use parameter margins
for sum values in pivot_table
然后将所有值最后一行 All
除以 div
:
IIUC you can use parameter margins
for sum values in pivot_table
and then divide all values last row All
by 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
用 除以个体计数的解决方案div
和 多
:
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
这篇关于Pandas:用百分比制作数据透视表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文