本文介绍了 pandas 将列转换为总数的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框,其中包含4列ID和3个类别,结果属于
I have a dataframe with 4 columns an ID and three categories that results fell into
<80% 80-90 >90
id
1 2 4 4
2 3 6 1
3 7 0 3
我想将其转换为百分比,即:
I would like to convert it to percentages ie:
<80% 80-90 >90
id
1 20% 40% 40%
2 30% 60% 10%
3 70% 0% 30%
这似乎应该在熊猫的能力范围之内,但我只是想不通.
this seems like it should be within pandas capabilities but I just can't figure it out.
提前谢谢!
推荐答案
您可以使用基本的熊猫运算符.div
和.sum
,并使用axis
参数来确保计算按照您希望的方式进行:
You can do this using basic pandas operators .div
and .sum
, using the axis
argument to make sure the calculations happen the way you want:
cols = ['<80%', '80-90', '>90']
df[cols] = df[cols].div(df[cols].sum(axis=1), axis=0).multiply(100)
- 计算每列的总和(
df[cols].sum(axis=1
).axis=1
使求和发生在各行中,而不是在各列下进行. - 将数据帧除以所得的序列(
df[cols].div(df[cols].sum(axis=1), axis=0
).axis=0
使划分跨列进行. - 最后,将结果乘以
100
,以便它们是0到100之间的百分比,而不是0到1之间的比例(或者您可以跳过此步骤并将它们存储为比例). - Calculate the sum of each column (
df[cols].sum(axis=1
).axis=1
makes the summation occur across the rows, rather than down the columns. - Divide the dataframe by the resulting series (
df[cols].div(df[cols].sum(axis=1), axis=0
).axis=0
makes the division happen across the columns. - To finish, multiply the results by
100
so they are percentages between 0 and 100 instead of proportions between 0 and 1 (or you can skip this step and store them as proportions).
这篇关于 pandas 将列转换为总数的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!