我有一个数据框:
Date_1 Date_2 is_B weight_1
01/09/2019 02/08/2019 1 254
01/09/2019 02/08/2019 1 320
01/09/2019 04/08/2019 1 244
01/09/2019 04/08/2019 1 247
01/09/2019 14/08/2019 0 343
01/09/2019 14/08/2019 1 161
01/09/2019 14/08/2019 1 386
01/09/2019 15/08/2019 1 465
01/09/2019 15/08/2019 1 133
01/09/2019 15/08/2019 1 310
01/09/2019 15/08/2019 1 155
我想生成一列new_weight,以便对于每个date_1,new_weight的值为5000-weight_1,其中is_B值为1。如果is_B = 0,则将较旧的new_weight值复制到new_weight中。
我知道要计算new_weight,我们可以做到:
df = 5000 - df.groupby('date_1')['weight_1'].cumsum()
但是我不确定如何在代码中应用is_b的条件。
有人能建议用熊猫还是麻木的方式做同样的事吗?
编辑
预期输出
Date_1 Date_2 is_B weight_1 new_weight
01/09/2019 02/08/2019 1 254 5000-254
01/09/2019 02/08/2019 1 320 5000-254-320
01/09/2019 04/08/2019 1 244 5000-254-320-244
01/09/2019 04/08/2019 1 247 5000-254-320-244-247
01/09/2019 14/08/2019 0 343 5000-254-320-244-247(we won't subtract 343 as isBooked = 0)
01/09/2019 14/08/2019 1 161 .
01/09/2019 14/08/2019 1 386 .
01/09/2019 15/08/2019 1 465 .
01/09/2019 15/08/2019 1 133 .
01/09/2019 15/08/2019 1 310 .
01/09/2019 15/08/2019 1 155 .
谢谢