user13140439
user13140439

Reputation:

calculation of Balance using Credit debit in pandas

I have Dataframe like this

openingbalance = 10.00
    Date         Credit Debit 
0   01/09/2020   15.00   0.00 
1   02/09/2020   0.00    5.00
2   03/09/2020   0.00    5.00

I want DataFrame like this

      Date       Credit Debit   Balance
0   01/09/2020   15.00   0.00   25.00
1   02/09/2020   0.00    5.00   20.00
2   03/09/2020   0.00    5.00   15.00

First Balance value is 25.00 because openingbalance is 10.00 and first value is credit 15.00 so 10.00+ 15.00 ,if first value is debit then 10.00 - First Debit value

please help

Upvotes: 0

Views: 717

Answers (1)

Chris Adams
Chris Adams

Reputation: 18647

Subtract Debits from Credits and use Series.cumsum on the result, then just add this to openingbalance to get Balance:

df['Balance'] = openingbalance + (df['Credit'] - df['Debit']).cumsum()

[out]

         Date  Credit  Debit  Balance
0  01/09/2020    15.0    0.0     25.0
1  02/09/2020     0.0    5.0     20.0
2  03/09/2020     0.0    5.0     15.0

Upvotes: 2

Related Questions