Add percent of total column to Pandas pivot_table

2020-06-28 06:28发布

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}

标签: python pandas
2条回答
干净又极端
2楼-- · 2020-06-28 07:14

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

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)

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(...

查看更多
狗以群分
3楼-- · 2020-06-28 07:28

The most natural way is to do it as you create the pivot table. Here I assume that date_submitted is a column (not in the index) using reset_index. And make sure that your values are in a column (here I call that 'value_col'). Then

def calc_group_pct(df,value_var = 'value_col'):
    df['pct'] = df[value_var]/float(df[value_var].sum())
    return df
df.groupby('date_submitted').apply(calc_group_pct)
查看更多
登录 后发表回答