Python 列上的累计和和百分比?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20965046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cumulative sum and percentage on column?
提问by ComputerFellow
I have a DataFramelike this:
我有一个DataFrame这样的:
df:
df:
fruit val1 val2
0 orange 15 3
1 apple 10 13
2 mango 5 5
How do I get Pandas to give me a cumulative sum and percentage column on only val1?
我如何让 Pandas 只给我一个累积总和和百分比列val1?
Desired output:
期望的输出:
df_with_cumsum:
df_with_cumsum:
fruit val1 val2 cum_sum cum_perc
0 orange 15 3 15 50.00
1 apple 10 13 25 83.33
2 mango 5 5 30 100.00
I tried df.cumsum(), but it's giving me this error:
我试过了df.cumsum(),但它给了我这个错误:
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
类型错误:输入类型不支持 ufunc 'isnan',并且无法根据转换规则 ''safe'' 将输入安全地强制转换为任何受支持的类型
采纳答案by BrenBarn
df['cum_sum'] = df['val1'].cumsum()
df['cum_perc'] = 100*df['cum_sum']/df['val1'].sum()
This will add the columns to df. If you want a copy, copy dffirst and then do these operations on the copy.
这会将列添加到df. 如果要副本,df请先复制,然后在副本上执行这些操作。
回答by Gene
It's a good answer, but written in 2014. I just modified a little bit, so it can pass the compiler and results looks similar to the example.
这是一个很好的答案,但写于 2014 年。我只是修改了一点,所以它可以通过编译器,结果看起来与示例类似。
df['cum_sum'] = df["val1"].cumsum()
df['cum_perc'] = round(100*df.cum_sum/df["val1"].sum(),2)

