【问题标题】:Iterate over column to convert currency with the forex-python package使用 forex-python 包遍历列以转换货币
【发布时间】:2020-12-24 17:35:15
【问题描述】:

我有一列 ['currency'] 和另一列 ['amount']。 ['currency'] 列包含以下格式的对象:USD、EUR、SEK、AUD 等。

我想通过转换所有其他货币将所有 ['amount'] 值转换为美元,并在新列中打印新的转换,但我在这一点上卡住了:

    import pandas as pd
    from forex_python.converter import CurrencyRates

    c = CurrencyRates()

    y = ['currency']
    x = ['amount']

    for column in df['amount']:
       c.get_rate('USD', x)

我应该将每个货币符号与相应的金额联系起来吗?

【问题讨论】:

  • 你的数据集在哪里?是熊猫吗?
  • @gtomer 是的,对不起。来自 excel 文件的 Pandas df。

标签: python pandas currency


【解决方案1】:

如果您有 df,那么这是迭代行的方法:

for index, row in df.iterrows():
    c.get_rate(row['currency'], row['amount'])

我不熟悉get_rate函数。

【讨论】:

  • 好的,谢谢。但是 ['amount'] 中的值取决于 ['currency'] 中的字符串,即它需要知道它正在转换什么货币。
  • 那我猜应该是:c.get_rate(row['currency'], row['amount'])
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 2017-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多