Raul Gonzales
Raul Gonzales

Reputation: 906

conditional statement producing weird results?

I want to create a new feature that converts and currency to EUR. the process is simple. I have 2 columns, one with the type of currency i.e USD and then the amount on the other column. I am creating a 3rd column called 'price_in_eur' and what is does is looks at the type of currency and if it is not 'EUR' then it should multiply the type of currency column by 1.1, otherwise it should be left alone but when i run the code i get the following error: ValueError: either both or neither of x and y should be given this is my code:

 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)

Can someone please help? I think it has something to do with the fact that the np.where statement is looking at the type_of_currency column which is a string but not sure.

Upvotes: 1

Views: 47

Answers (1)

Rakesh
Rakesh

Reputation: 82785

You need to provide both arguments in np.where after condition. --> numpy.where(condition[, x, y])

Ex:

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

MoreInfo

Upvotes: 4

Related Questions