使用 easymoney 和 pandas 进行货币转换

Currency conversion with easymoney and pandas

我正在尝试将不同货币的值转换为 "USD" 货币。我尝试了 easymoneyCurrencyConvertor 包,但它们似乎不适用于数据框 python.

如果我使用 iloc 逐行转换似乎可行,但这会花费大量时间。

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

您需要 applyaxis=1 才能按行处理:

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)