1 回答

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊
# use df.where based on your condition and assign it to a new col
# Anywhere column condition is True return the value else return NaN
# then add ffill to forward fill NaN values
df['r'] = df['value'].where(df['condition'] == True, np.nan).ffill()
date value condition desired_result r
0 2000-01-01 10.0 False NaN NaN
1 2000-01-02 11.0 False NaN NaN
2 2000-01-03 12.0 True 12.0 12.0
3 2000-01-04 13.0 True 13.0 13.0
4 2000-01-05 14.0 False 13.0 13.0
5 2000-01-06 15.0 True 15.0 15.0
6 2000-01-07 16.0 False 15.0 15.0
7 2000-01-08 17.0 False 15.0 15.0
8 2000-01-09 18.0 True 18.0 18.0
9 2000-01-10 19.0 False 18.0 18.0
添加回答
舉報(bào)