2014-01-07 199 views
27

我有一個DataFrame這樣的:列的累積總數和百分比?

df

fruit val1 val2 
0 orange 15 3 
1 apple  10 13 
2 mango  5 5 

如何讓熊貓給我的累加值和百分比列只val1

所需的輸出:

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 

我試過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''

回答

57
df['cum_sum'] = df.val1.cumsum() 
df['cum_perc'] = 100*df.cum_sum/df.val1.sum() 

這將列添加到df。如果您想要副本,請首先複製df,然後在副本上執行這些操作。

+0

'TypeError:不支持的操作數類型爲*:'int'和'instancemethod''爲第二行 – ComputerFellow

+3

您是否輸入'cumsum'或'cum_sum'? – BrenBarn

+11

哦,你的天才! – ComputerFellow