将总列的百分比添加到Pandasivot_table [英] Add percent of total column to Pandas pivot_table
问题描述
我似乎无法弄清楚如何将每个date_submitted组的总列的百分比添加到下面的pandas数据透视表中:
I can't seem to figure out how to add a % of total column for each date_submitted group to the below pandas pivot table:
In [177]: pass_rate_pivot
date_submitted audit_status
04-11-2014 audited 140
is_adserver 7
rejected 75
unauditable 257
04-18-2014 audited 177
is_adserver 10
pending 44
rejected 30
unauditable 226
04-25-2014 audited 97
is_adserver 5
pending 33
rejected 9
unauditable 355
Name: site_domain, dtype: int64
In [177]: pass_rate_pivot.to_dict()
Out[177]:
{('04-11-2014', 'audited'): 140,
('04-11-2014', 'is_adserver'): 7,
('04-11-2014', 'rejected'): 75,
('04-11-2014', 'unauditable'): 257,
('04-18-2014', 'audited'): 177,
('04-18-2014', 'is_adserver'): 10,
('04-18-2014', 'pending'): 44,
('04-18-2014', 'rejected'): 30,
('04-18-2014', 'unauditable'): 226,
('04-25-2014', 'audited'): 97,
('04-25-2014', 'is_adserver'): 5,
('04-25-2014', 'pending'): 33,
('04-25-2014', 'rejected'): 9,
('04-25-2014', 'unauditable'): 355}
推荐答案
这是您想要的吗? (对于将元素除以该组中所有元素之和的每个组):
Is this what you want? (for each group dividing the element with the sum of all elements in that group):
In [62]: pass_rate_pivot.groupby(level=0).transform(lambda x: x/x.sum())
Out[62]:
04-11-2014 audited 0.292276
is_adserver 0.014614
rejected 0.156576
unauditable 0.536534
04-18-2014 audited 0.363450
is_adserver 0.020534
pending 0.090349
rejected 0.061602
unauditable 0.464066
04-25-2014 audited 0.194389
is_adserver 0.010020
pending 0.066132
rejected 0.018036
unauditable 0.711423
dtype: float64
如果要将其添加为列,实际上可以按照@ exp1orer的建议将concat
两个系列都添加到一个数据框中:
If you want to add this as a column, you can indeed concat
both serieses to one dataframe as suggested by @exp1orer:
pd.concat([pass_rate_pivot,pass_rate_pivot_pct], axis=1)
如果pass_rate_pivot
已经是一个数据框,则只需分配一个新列,例如pass_rate_pivot['pct'] = pass_rate_pivot['original column'].groupby(...
If pass_rate_pivot
would already be a dataframe, you could just assign a new column like pass_rate_pivot['pct'] = pass_rate_pivot['original column'].groupby(...
这篇关于将总列的百分比添加到Pandasivot_table的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!