0

Let sat I have a DataFrame with column A.

A= (1,2,3,4,5,6...n)

I want to create column B like this:

B=(1,3,6,10,15,21...n)

Explicitly: i+(sum of all the previous numbers)

Probably simple, but hard for me:P Very new to programming Thanks!

Bhbf
  • 157
  • 13
  • 1
    `B = np.cumsum(A)` `import numpy as np` – ombk Nov 28 '20 at 12:57
  • Does this answer your question? [Sum of previous rows values](https://stackoverflow.com/questions/43396855/sum-of-previous-rows-values) – Rajat Mishra Nov 28 '20 at 12:58
  • 2
    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) – FloLie Nov 28 '20 at 13:01

1 Answers1

1
from itertools import accumulate

A = [1, 2, 3, 4, 5, 6]
B = list(accumulate(A)) #->[1, 3, 6, 10, 15, 21]
apech zzz
  • 25
  • 4