Home > front end >  How to create a python function for the below requirement
How to create a python function for the below requirement

Time:08-24

I need to create a python function for which I need to subtract a value from previous row and the first row for all the account id will be 1. I attach the image of excel file need to calculate Column D as per the working in the excel sheet.

I will be having the values for column A,B,C and from this need to calculate Column D as per the working in the excel sheet.

Formula to arrive Column D is D[i-1]-C[i]-B[i].

Note: For all the account id the first row need to be subtracted by 1 as indicated in column D4.

Formula for D5 Cell is D4-C5-B5 and D6 Cell is D5-C6-B6 and for cell D7 is D6-C7-B7 and so on.

Data

Sample Data

Thanks, Karthik.

CodePudding user response:

import pandas as pd

df = pd.DataFrame([
    [1, 0.0011, 0.0350],
    [2, 0.0017, 0.0370],
    [3, 0.0032, 0.0390],
    [4, 0.0055, 0.0430],
], columns=['A', 'B', 'C'])
df['D'] = 1 - (df.C   df.B).cumsum()
df

prints

index A B C D
0 1 0.0011 0.035 0.9639
1 2 0.0017 0.037 0.9252
2 3 0.0032 0.039 0.883
3 4 0.0055 0.043 0.8345
  • Related