条件语句产生奇怪的结果?

时间:2019-11-25 11:57:35

标签: python numpy

我想创建一个将货币和货币转换为EUR的新功能。过程很简单。我有2列,一个列的是货币类型,即美元,然后是另一列的金额。我正在创建一个名为“ price_in_eur”的第三列,它的作用是查看货币类型,如果不是“ EUR”,则应将货币列的类型乘以1.1,否则应单独使用,但是当我运行代码,我得到以下错误: ValueError: either both or neither of x and y should be given这是我的代码:

 x = data[['type_of_currency','amount']]
 x.type_of_currency= x.amount.str.extract('(\d+)', expand=False)
 x['type_of_currency'] = x['amount'].astype(float)
 x['price_in_euro'] = np.where(x['type_of_currency']=='USD',x['amount']*1.1)

有人可以帮忙吗?我认为这与以下事实有关:np.where语句正在查看type_of_currency列,该列是字符串但不确定。

1 个答案:

答案 0 :(得分:4)

您需要在条件后的np.where中提供两个参数。 -> numpy.where(condition[, x, y])

例如:

x['price_in_euro'] = np.where(x['type_of_currency']=='USD',x['amount']*1.1, x['amount'])

MoreInfo