2 回答
TA贡献1789条经验 获得超8个赞
用:
df['Cum_Prob'] = df.iloc[:, 1:].sum(axis=1)
或
df['Cum_Prob'] = df[df.columns[df.columns.str.contains('Day')]].sum(axis=1)
编辑
df_days = df[df.columns[df.columns.str.contains('Day')]]
cumprob=0
for i, col in df_days.items():
cumprob = col.mul(1-cumprob) + cumprob
df['Cum_Prob']=cum_Prob
输出
Id Day1 Day2 Day3 Cum_Prob
0 1 0.35 0.32 0.29 0.686180
1 2 0.63 0.59 0.58 0.936286
2 3 0.12 0.10 0.07 0.263440
具有减少的替代方案
from functools import reduce
df['Cum_Prob']=reduce(lambda cum_prob, new_prob: (1-cum_prob)*new_prob + cum_prob ,
df_days.values.T)
减少可能是最快的
%%timeit
from functools import reduce
df['Cum_Prob']=reduce(lambda cum_prob, new_prob: (1-cum_prob)*new_prob + cum_prob ,
df_days.values.T)
111 µs ± 2.29 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%%timeit
cumprob=0
for i, col in df_days.items():
cumprob = col.mul(1-cumprob) + cumprob
df['Cum_Prob']=cumprob
1.44 ms ± 5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
TA贡献1886条经验 获得超2个赞
只要算一下,这只是
1 - (1-df).prod(1)
# if your `Id` is not index:
# 1 - df.filter(like='days)
# 1 - df.set_index('Id')
输出:
Id
1 0.686180
2 0.936286
3 0.263440
dtype: float64
- 2 回答
- 0 关注
- 218 浏览
添加回答
举报