sshah98
sshah98

Reputation: 352

How to subtract numbers in 1 column of a pandas dataframe?

Currently, I am using Pandas and created a dataframe that has two columns:

Price         Current Value
1350.00       0
1.75          0
3.50          0
5.50          0

How Do I subtract the first value, and then subtract the sum of the previous two values, continuously (Similar to excel) like this:

Price         Current
1350.00       1350.00
1.75          1348.25
3.50          1344.75
5.50          1339.25

How can this be done for more than just four rows?

Upvotes: 1

Views: 1092

Answers (1)

BENY
BENY

Reputation: 323366

This will achieve what you need , cumsum

1350*2-df.Price.cumsum()
Out[304]: 
0    1350.00
1    1348.25
2    1344.75
3    1339.25
Name: Price, dtype: float64

After assign it back

df.Current=1350*2-df.Price.cumsum()
df
Out[308]: 
     Price  Current
0  1350.00  1350.00
1     1.75  1348.25
2     3.50  1344.75
3     5.50  1339.25

Upvotes: 2

Related Questions