基于列值的百分比减少

Percentage decrease based on column value

我的数据框如下所示:

question   timeSpent
a          5354
b          2344
c          2555
d          5200
e          3567

我想添加一个额外的列 Score,其中包含介于 01 之间的值。 timeSpent(以秒表示)越大,Score越接近0。如果花费的时间更小,那么 Score 接近 1.

如果 timeSpent 小于或等于 2500,则假设值为 1。然后每 100 秒下降 20%。如果命中或大于 5500,则停留在 0

因此 2600 的得分为 0.82700 的得分为 0.64

我为每个间隔写了 if-else 语句,但我认为必须有更快的方法来完成。

您可以创建一个函数来计算分数并将其应用于每个 timeSpent

def get_score(num):
    if num <= 2500: return 1
    if num >= 5500: return 0
    x = 1
    for _ in range((num - 2500) // 100):
        x *= 0.8
    return x

df = pd.DataFrame({'question': [a, b, c, d, e], 'timeSpent': [5354, 2344, 2555, 5200, 3567]})
df['Score'] = df.timeSpent.apply(lambda x: get_score(x))

输出:

  question  timeSpent     Score
0        a       5354  0.001934
1        b       2344  1.000000
2        c       2555  1.000000
3        d       5200  0.002418
4        e       3567  0.107374