2

I have something that has been bugging me for a while.

I have a Dataframe, looking like this:

           Amount  Balance       Date                           Text
90    -442.378339     5000 2018-02-12     PS Bar               04066
502   -183.229472     5000 2018-08-14               VDK EUR    39,95
957    -35.117568     5000 2019-01-28  Fakta Strandlodsve )))) 59260
1334   -15.234783     5000 2019-06-28                          Rente
1867  2934.846978     5000 2019-12-09           Netbank overf�rsel
2304   -30.299886     5000 2019-05-21            MobilePay: Cathrine
2341    21.209920     5000 2019-06-03            MOBILEPAY: KRISTIAN
2357   -90.505760     5000 2019-06-04     DK-NOTA68957 NETTO GODTHÅB
2447  -182.799214     5000 2019-07-03               Forretning: REAL
2955  -121.199545     5000 2019-12-30     DK-NOTA17767 WANNASPORT.DK

I want to update the Balance column based on the Amount column. The balance should start at 5000, but for the second row, the balance should be 5000 - 442,378339 = 4557,62, and for the third row, it should be 4557,62 - 183,229472 = 4374,39.

I have tried df['Balance'] = df['Balance'] + df['Amount'].shift(1)

The second row is correct, but after that it just keeps taking the 5000, and not the previous computed value of Balance.

2
  • df['Balance'] - df['Amount'].cumsum() or df['Balacnce'] + df['Amount'].cumsum() Commented Feb 17, 2020 at 20:30
  • 1
    I knew it was something simple - thank you so much Scott! If you post an answer, I will approve it :) Commented Feb 17, 2020 at 20:32

1 Answer 1

1

Let's use cumsum on 'Amount' column:

Try:

df['Balance'] + df['Amount'].cumsum()

Output:

90      4557.621661
502     4374.392189
957     4339.274621
1334    4324.039838
1867    7258.886816
2304    7228.586930
2341    7249.796850
2357    7159.291090
2447    6976.491876
2955    6855.292331
dtype: float64
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.