Home > Blockchain >  update a value by running through every row in a data frame with conditions (extension)
update a value by running through every row in a data frame with conditions (extension)

Time:12-06

This is an extension to the question, 'update a value by running through every row in a data frame with conditions' enter image description here

Desired output:

Df:

Index A B A_yes B_yes Points
0 2.43 1.55 1 0 875.5
1 2.58 1.49 0 1 1067.5875
2 1.61 2.32 1 0 1393.2017
3 2.7 1.46 1 0 2557.42324

CodePudding user response:

You could try the following with df your dataframe:

start = 500
weights = (df["A"].where(df["A_yes"].eq(1), df["B"]) * 0.5   0.5).cumprod()
df["Points"] = start * weights
  • Use .where to select between the value in A or B based on the A_yes and B_yes entries (my assumption here is that there's a 1 either in A_yes or B_yes, but not in both).
  • Calculate the average between the selected value and 1.
  • Build the actual weights with .cumprod.
  • Multiply the weights with the start value to get the Points.

Result for df =

      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.70  1.46      1      0

is

      A     B  A_yes  B_yes       Points
0  2.43  1.55      1      0   857.500000
1  2.58  1.49      0      1  1067.587500
2  1.61  2.32      1      0  1393.201688
3  2.70  1.46      1      0  2577.423122
  • Related