我有一个包含3列的pandas数据帧。
Event_occur Boolean
Event_predict Boolean
Incorrect_pred Number default 0
请参阅截图。我试图根据某些条件更新Incorrect_pred。
如果这是sql,我可以使用windows函数。类似的东西:
(case
when Event_occur = 'FALSE' then 0
else sum(case when Event_occur = Event_predict) then 0 else 1 end)
over (order by <some column>) end)
我有可能在熊猫中做到这一点吗?
答案 0 :(得分:0)
让我们试试:
df['pred'] = np.where(df.Event_occur == False,0,np.where(df.Event_occur != df.Event_predict,1,0)).cumsum()
df['Incorrect_pred']= df.pred.where(df.Event_occur == True).fillna(0)
print(df)
输出:
Event_occur Event_predict Incorrect_pred pred
0 False True 0.0 0
1 True False 1.0 1
2 True False 2.0 2
3 False False 0.0 2
4 True False 3.0 3
5 True True 3.0 3
6 True True 3.0 3
7 True False 4.0 4
8 False True 0.0 4
9 True True 4.0 4