简体   繁体   English

Python/Pandas 将 pivot 表转换为基于行总数的百分比

[英]Python/Pandas convert pivot table into percentages based on row total

I think this should be simple, but in Python I just can't figure out how to take a pivot table and get percentages based on row totals.我认为这应该很简单,但在 Python 中,我只是想不通如何使用 pivot 表并根据行总数获取百分比。

Pivot Table: Pivot表:

df.pivot_table(index='Name', columns='scenario',
               values='y', aggfunc=np.count_nonzero, margins=True, margins_name='Total').fillna(0)

Which gets:哪个得到:

在此处输入图像描述

But what I want is each cell divided by row totals:但我想要的是每个单元格除以行总数:

在此处输入图像描述

How can I do this?我怎样才能做到这一点? Thank you in advance.先感谢您。

  1. You can use the .pipe method combined with .div to perform this column-wise division on all of the columns.您可以结合使用.pipe方法和.div对所有列执行按列划分。
  2. You can then use .applymap to apply a string fomratting to each of your values to get the values to appear as percentages (note that they become strings and are no longer mathematically useful)然后,您可以使用.applymap将字符串格式应用于每个值,以使值显示为百分比(请注意,它们变成字符串并且在数学上不再有用)
out = (
    df.pivot_table(
        index='Name', columns='scenario', values='y', 
        aggfunc=np.count_nonzero, margins=True, 
        margins_name='Total', fill_value=0
    )
    .pipe(lambda d: d.div(d['Total'], axis='index'))
    .applymap('{:.0%}'.format)
)

example例子

df = pd.DataFrame({
    'a': [1, 0, 0, 1, 5],
    'b': [20, 20, 10, 50, 15], 
   'c': [50, 20, 50, 100, 20]
})

print(df)
   a   b    c
0  1  20   50
1  0  20   20
2  0  10   50
3  1  50  100
4  5  15   20


out = (
    df.pipe(lambda d: d.div(d['c'], axis='index'))
    .applymap('{:.0%}'.format)
)

print(out)
     a     b     c
0   2%   40%  100%
1   0%  100%  100%
2   0%   20%  100%
3   1%   50%  100%
4  25%   75%  100%

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

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