KubiK888
KubiK888

Reputation: 4723

How to populate subsequent rows based on previous row value and value from another column in Python Pandas?

I have the following df.

cases  percent_change
100    0.01
NaN    0.00
NaN    -0.001
NaN    0.05

For the next rows (starting in the second row) from the cases column, it's calculated as next cases = previous cases * (1 + previous percent_change), or for the row below the 100, it is calculated as 100 * (1 + 0.01) = 101. Thus, it should populate like so

cases  percent_change
100    0.01
101    0.00
101    -0.001
100.899    0.05

I want to ignore the first row (or 100). Here is my code which is not working

df.loc[1:, 'cases'] = df['cases'].shift(1) * (1 + df['percent_change'].shift(1))

Tried this as well with no success

df.loc[1:, 'cases'] = df.loc[1:, 'cases'].shift(1) * (1 + df.loc[1:, 'percent_change'].shift(1))

Upvotes: 0

Views: 65

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195418

df['cases'] = (df.percent_change.shift(1).fillna(0) + 1).cumprod() * df.at[0, 'cases']
print(df)

Prints:

     cases  percent_change
0  100.000           0.010
1  101.000           0.000
2  101.000          -0.001
3  100.899           0.050

Upvotes: 2

Related Questions