关于pandas条件的Loc函数返回Nan

时间:2017-09-08 17:41:33

标签: python pandas dataframe loc

我真的不明白为什么它会返回NaN

我有一个df,我需要根据其他列值再创建一个列,此方法始终有效。

train.loc[(train.region == 'Latin America') & (train.sales_channel == 'Online'), 'expense'] = 47
train.loc[(train.region == 'Latin America') & (train.sales_channel == 'Partnerships'), 'expense'] = 63
train.loc[(train.region == 'Latin America') & (train.sales_channel == 'Direct Sales'), 'expense'] = 89
train.loc[(train.region == 'Europe') & (train.sales_channel == 'Online'), 'expense'] = 69

我回来了

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试以下

train.loc[(train['region'] == 'Latin America') & (train['sales_channel'] == 'Partnerships'),['expense']] = 63