2016-10-11 67 views
0

如果某些条件为真,我试图将值从0更新为值1。这里是我的代码:有条件更新数据帧列

df_['win'] = 0 
df_.loc[df_['predicted_spread'] > df_['vegas_spread'] and df_['actual_spread'] > df_['vegas_spread'], 'win'] = 1 

这是我得到的错误:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). 

有谁知道这是怎么回事上的任何想法?

回答

1

在做布尔索引,你需要使用逻辑运算符为and这是&。看到该文档here更多信息

df['win'] = 0 
cond_1 = df['predicted_spread'] > df['vegas_spread'] 
cond_2 = df['actual_spread'] > df['vegas_spread'] 
df.loc[cond_1 & cond_2, 'win'] = 1 

基本上只是你的答案与&而不是and。我只是重写了它,使其更清晰一些。

+0

出色地工作!谢谢! – user3294779

1

尝试

df_['win'] = 0 
df_.loc[df_['predicted_spread'].gt(df_['vegas_spread']) and df_['actual_spread'].gt(df_['vegas_spread']), 'win'] = 1