Home > Enterprise >  update a value by running through every row in a data frame with conditions
update a value by running through every row in a data frame with conditions

Time:12-02

I have a starting balance of 'Points = 500', and as I run through this data frame, the point value will update as it goes through every row. Here is the data frame:

Index A B A_yes B_yes
0 2.43 1.55 1 0
1 2.58 1.49 0 1
2 1.61 2.32 1 0
3 2.7 1.46 1 0

What I'm trying to do is update 'Points' based on the following conditions for every row:

For example, row 1:

If A_yes = 1 then multiply the current 'Points' value by the first row in the 'A' column (2.43). (Otherwise if it was B_yes that equalled 1, then the 'Points' value would have been multiplied by the first row in the 'B' column (1.55)).

So Points = 500 * 2.43 = 1215

Second row:

Now that Points = 1215, this will be used for the next row and the same process will be followed, If A_yes = 1 then multiply 'Points' by the value in the A column (2.58), however because A_yes = 0, then if B_yes = 1 then multiply 'Points' by the 'B' column (1.49) and updated 'Points' now = 1810.35

I'm new to pandas and not sure how to create a function that will run through all the rows and update the 'Points' value in this way

CodePudding user response:

You can multiple columns A,B by Yes columns, then multiple first row by 500, then sum both columns and add cumulative product by Series.cumprod:

cols = ['A','B']

df1 = df[['A','B']].mul(df[['A_yes','B_yes']].to_numpy())
df1.loc[0, cols] *= 500

df['Point'] = df1.sum(axis=1).cumprod()
print (df)
      A     B  A_yes  B_yes       Point
0  2.43  1.55      1      0  1215.00000
1  2.58  1.49      0      1  1810.35000
2  1.61  2.32      1      0  2914.66350
3  2.70  1.46      1      0  7869.59145
  • Related