Converting currency in pandas

I am trying to convert values ​​with a different currency to the "USD" currency. I've tried packages easymoney

and CurrencyConvertor

, but they don't seem to work with python dataframe.

Seems to work if I do the conversion line by line with iloc

, but it takes a very long time.

from easymoney.money import EasyPeasy
ep = EasyPeasy()
ep.currency_converter(df_train['goal'], from_currency=df_train['currency'], to_currency="USD")

      

Error:
TypeError: Invalid type comparison

+3


source to share


1 answer


You need apply

c axis=1

to process line by line:



from easymoney.money import EasyPeasy 
ep = EasyPeasy() 
df_train['converted'] = df_train.apply(lambda x: ep.currency_converter(x['goal'], from_currency=x['currency'], to_currency="USD"), axis=1)

      

+1


source







All Articles