Cumulative sum and percentage on column?

后端 未结 2 1273
野的像风
野的像风 2020-11-29 22:14

I have a DataFrame like this:

df:

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


        
相关标签:
2条回答
  • 2020-11-29 22:34

    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.

    df['cum_sum'] = df["val1"].cumsum()
    df['cum_perc'] = round(100*df.cum_sum/df["val1"].sum(),2)
    
    0 讨论(0)
  • 2020-11-29 22:43
    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.

    0 讨论(0)
提交回复
热议问题