Currency conversion in pandas

戏子无情 提交于 2019-12-07 07:10:26

问题


I am trying to convert values with different currency to "USD" currency. I tried easymoney and CurrencyConvertor packages but those do not seem to work with dataframe python.

It seems working if I do conversion row by row using iloc but that is taking an awful lot of 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


回答1:


You need apply with axis=1 for processing by rows:

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)


来源:https://stackoverflow.com/questions/44618142/currency-conversion-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!