Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cumulative sum and percentage on column?

I have a DataFrame like this:

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?

Desired output:

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:

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''

like image 484
ComputerFellow Avatar asked Jan 07 '14 06:01

ComputerFellow


People also ask

How do you do a cumulative percentage in Excel?

⇒ Drag or fill down the Cell C2 to C11 with the Fill Handle option. ⇒ Go to Cell D2, type =C2; it'll copy the value from Cell C2. ⇒ Now select Cell D2 & add C3 & D2 Cells. ⇒ Fill Down the Cell D3 to D11 & you'll get all cumulative percentage values right away.

How do you calculate cumulative percentage?

The Cumulative percentage column divides the cumulative frequency by the total number of observations (in this case, 25). The result is then multiplied by 100. This calculation gives the cumulative percentage for each interval.

What is cumulative percentage vs percentage?

A simple way for remembering a percentage is that it shows a part of the whole. Cumulative percentages add a percentage from one period to the percentage of another period. This calculation is important in statistics because it shows how the percentages add together over a time period.


1 Answers

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 df first and then do these operations on the copy.

like image 57
BrenBarn Avatar answered Sep 22 '22 07:09

BrenBarn