Pandas, Cross summation with initial lag

Question:

I know something has been written about this but I don’t see a real solution other than writing line by line.
I would like to do this:

import pandas as pd
df = pd.DataFrame([[1],[2],[4]], columns=['A'] )

B1 = A1 if A1>0 else 0
C1 = A1
B2 = A2+C1 if A2>0 else 0
C2 = C1+B2
B3 = A3+C2 if A3>0 else 0
C3 = C2+ B3
...
Asked By: Dejan Skolegi

||

Answers:

import pandas as pd

df = pd.DataFrame([[1],[2],[4]], columns=['A'] )

# Create new columns filled with None
df['B'] = None
df['C'] = None

# Initialize new columns for the first row
df.loc[0, 'B'] = max((0, df.loc[0, 'A']))
df.loc[0, 'C'] = df.loc[0, 'A']

# Fill next rows' values based on previous rows' values
for i in range(1, len(df)):
    df.loc[i, 'B'] = (df.loc[i, 'A'] + df.loc[i - 1, 'C']) if df.loc[i, 'A'] > 0 else 0
    df.loc[i, 'C'] = df.loc[i, 'B'] + df.loc[i - 1, 'C']

Will output

   A  B   C
0  1  1   1
1  2  3   4
2  4  8  12
Answered By: Alex Bochkarev
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.