2017-06-18 98 views
3

我試圖將不同貨幣的值轉換爲「美元」貨幣。我嘗試了easymoneyCurrencyConvertor包,但這些包似乎不適用於dataframe 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

回答

1

您需要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) 
+0

你如何處理與'null'值?我一直收到錯誤''不能將浮點數NaN轉換爲整數'' – kevinaskevin

+0

@kevinaskevin - 通常有兩種方法 - 用'fillna()'替換'NaN'或'None's爲一些整數或者用'dropna ' – jezrael