pandas DataFrame货币转换 [英] Pandas DataFrame currency conversion

查看:60
本文介绍了 pandas DataFrame货币转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两列的DataFrame:

I have DataFrame with two columns:

col1     | col2

20         EUR
31         GBP
5          JPY

我可能有10000行

如何快速将货币转换为基本货币英镑?

How to do fast currency conversion to base currency being GBP?

我应该使用easymoney吗?我知道如何将转换应用于单行,但我不知道如何快速遍历所有行.

should I use easymoney? I know how to apply conversion to single row but I do not know how to iterate through all the rows fast.

我想将某事应用为:

def convert_currency(amount, currency_symbol):
    converted = ep.currency_converter(amount=1000, from_currency=currency_symbol, to_currency="GBP")
    return converted


df.loc[df.currency != 'GBP', 'col1'] = convert_currency(currency_data.col1, df.col2
                                                                                  )

但它尚不可用.

推荐答案

df = pd.DataFrame([[20, 'EUR'], [31, 'GBP'], [5, 'JPY']], columns=['value', 'currency'])
print df

   value currency
0     20      EUR
1     31      GBP
2      5      JPY

def convert_to_gbp(args):  # placeholder for your fancy conversion function
    amount, currency = args
    rates = {'EUR': 2, 'JPY': 10, 'GBP': 1}
    return rates[currency] * amount

df.assign(**{'In GBP': df.apply(convert_to_gbp, axis=1)})

   value currency  In GBP
0     20      EUR      40
1     31      GBP      31
2      5      JPY      50

这篇关于 pandas DataFrame货币转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆