You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于Win/Lose列修改DataFrame的Points累积计算逻辑?

问题:根据Win/Lose列调整DataFrame的Points计算逻辑

这是此前问题《update a value by running through every row in a data frame with conditions (extension)》的进一步扩展。当前使用的DataFrame新增了Win/Lose列,数据如下:

IndexABA_yesB_yesWin/Lose
02.431.55101
12.581.49010
21.612.32101
32.71.46101

其中Win/Lose列中1代表获胜,0代表失败。

目前使用以下代码生成输出:

start = 500
weights = (df["A"].where(df["A_yes"].eq(1), df["B"]) * 0.5 + 0.5).cumprod()
df["Points"] = start * weights

生成的输出DataFrame如下:

IndexABA_yesB_yesPoints
02.431.5510875.5
12.581.49011067.5875
21.612.32101393.2017
32.71.46102557.42324

现需修改代码,使其遵循依赖Win/Lose列的计算规则,得到如下期望输出:

IndexABA_yesB_yesWin/LosePoints
02.431.55101857.5
12.581.49010428.75
21.612.32101707.4375
32.71.461011308.75912

解决方案

先拆解计算逻辑:

  • 先根据A_yes/B_yes选择对应的赔率(A或B)
  • 获胜时(Win/Lose=1):当前Points = 上一轮Points × (0.5×赔率 + 0.5)
  • 失败时(Win/Lose=0):当前Points = 上一轮Points × 0.5

可以用向量化写法实现(效率更高),代码如下:

import pandas as pd
import numpy as np

start = 500
# 选择对应赔率
odds = df["A"].where(df["A_yes"].eq(1), df["B"])
# 根据胜负计算每轮乘数
multipliers = np.where(df["Win/Lose"] == 1, 0.5 * odds + 0.5, 0.5)
# 计算累积乘积并乘以初始值
df["Points"] = start * multipliers.cumprod()

验证计算结果:

  • 第0行:500 × (0.5×2.43 + 0.5) = 857.5,符合期望
  • 第1行:857.5 × 0.5 = 428.75,符合期望
  • 第2行:428.75 × (0.5×1.61 + 0.5) = 707.4375,符合期望
  • 第3行:707.4375 × (0.5×2.7 + 0.5) ≈ 1308.75912,符合期望

内容的提问来源于stack exchange,提问作者monica

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 01:25:20