1

Is there an easy way to sum the value of all the rows above the current row in an adjacent column? Click on the image below to see what I'm trying to make. It's easier to see it than explain it.

Text explanation: I'm trying to create a chart where column B is either the sum or percent of total of all the rows in A that are above it. That way I can quickly visualize where the quartile, third, etc are in the dataframe. I'm familiar with the percentile function

but I'm not sure I can get it to do exactly what I want it to do. Image below as well as text version:

Text Version

1--1%
1--2%
4--6%
4--10%
2--12%

... and so on to 100 percent.

Do i need to write a for loop to do this?

Excel Chart:
enter image description here

jupiterbjy
  • 2,882
  • 1
  • 10
  • 28
user708503
  • 11
  • 1
  • 1
    Does this answer your question? [Cumsum as a new column in an existing Pandas data](https://stackoverflow.com/questions/41859311/cumsum-as-a-new-column-in-an-existing-pandas-data) – RichieV Aug 29 '20 at 02:53

1 Answers1

0

you can use cumsum for this:

import numpy as np
import pandas as pd
df = pd.DataFrame(data=dict(x=[13,22,34,21,33,41,87,24,41,22,18,12,13]))
df["percent"] = (100*df.x.cumsum()/df.x.sum()).round(1)

output:

     x  percent
0   13      3.4
1   22      9.2
2   34     18.1
3   21     23.6
4   33     32.3
5   41     43.0
6   87     65.9
7   24     72.2
8   41     82.9
9   22     88.7
10  18     93.4
11  12     96.6
12  13    100.0
anon01
  • 10,618
  • 8
  • 35
  • 58