【问题标题】:Currency conversion with easymoney and pandas使用easymoney和pandas进行货币转换
【发布时间】:2017-11-20 22:28:52
【问题描述】:

我正在尝试将不同货币的值转换为“美元”货币。我尝试了 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

【问题讨论】:

    标签: python pandas currency


    【解决方案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)
    

    【讨论】:

    • 你如何处理null 值?我不断收到错误'cannot convert float NaN to integer'
    • @kevinaskevin - 通常有两种方法 - 将 NaNs 或 Nones 替换为 fillna() 的某个整数或使用 dropna 删除行
    • 这对于 1m 行以上的数据帧来说运行速度非常慢。这是为什么呢?
    • @Snow - 问题是自定义函数,主要原因是解决方案未矢量化。
    猜你喜欢
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 2015-01-27
    • 2021-03-30
    • 1970-01-01
    • 2022-07-12
    • 2017-06-02
    • 1970-01-01
    相关资源
    最近更新 更多