This is an extension to the question, 'update a value by running through every row in a data frame with conditions'
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 inA
orB
based on theA_yes
andB_yes
entries (my assumption here is that there's a 1 either inA_yes
orB_yes
, but not in both). - Calculate the average between the selected value and 1.
- Build the actual
weights
with.cumprod
. - Multiply the
weights
with thestart
value to get thePoints
.
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