0

I have a column of numbers in a Python Pandas df: 1,8,4,3,1,5,1,4,2 If I create a cumulative sum column it returns the cumulative sum. How do I only return the rows that reaches a cumulative sum of 20 skipping numbers that take cumulative sum over 20?

+-----+-------+------+
| Var | total | cumu |
+-----+-------+------+
| a   |     1 |    1 |
| b   |     8 |    9 |
| c   |     4 |   13 |
| d   |     3 |   16 |
| e   |     1 |   17 |
| f   |     5 |   22 |
| g   |     1 |   23 |
| h   |     4 |   27 |
| i   |     2 |   29 |
+-----+-------+------+

Desired output:

+-----+-------+------+
| Var | total | cumu |
+-----+-------+------+
| a   |     1 |    1 |
| b   |     8 |    9 |
| c   |     4 |   13 |
| d   |     3 |   16 |
| e   |     1 |   17 |
| g   |     1 |   18 |
| i   |     2 |   20 |
+-----+-------+------+
Takos
  • 69
  • 9
Lulumocha
  • 143
  • 8
  • Does this answer your question? [Algorithm to find which number in a list sum up to a certain number](https://stackoverflow.com/questions/3420937/algorithm-to-find-which-number-in-a-list-sum-up-to-a-certain-number) – Błotosmętek May 08 '20 at 19:19
  • Thanks, this is more than I was looking for, still trying to understand it, I think this finds all the combinations of the numbers that add to a number. – Lulumocha May 08 '20 at 19:41

1 Answers1

1

If I understood your question correctly, you want only skip values that get you over cumulative sum of 20:

def acc(total):
    s, rv = 0, []
    for v, t in zip(total.index, total):
        if s + t <= 20:
            s += t
            rv.append(v)
    return rv

df = df[df.index.isin(acc(df.total))]
df['cumu'] = df.total.cumsum()
print(df)

Prints:

  Var  total  cumu
0   a      1     1
1   b      8     9
2   c      4    13
3   d      3    16
4   e      1    17
6   g      1    18
8   i      2    20
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91