SomeBruh
SomeBruh

Reputation: 460

Currency conversion by lookup in Pandas

In the dataframe I have price, currency and the conversion rate to a base currency

price | currency | GBP | EUR | CAD

I would live to convert the price to a base currency by looking up the conversion rates.

Currently I use a lambda function but the performance is very low for large data

df['usd'] = df.apply(lambda row: row['price'] / row[order.currency], axis=1)

What would be the best way to vectorise or improve this kind of lookup function? Thanks!

Upvotes: 0

Views: 398

Answers (1)

SomeBruh
SomeBruh

Reputation: 460

I found a much quicker solution.

df['fx_rate'] = df.lookup(df.index, df['currency'])
df['usd'] = df['price']/df['fx_rate']

Upvotes: 2

Related Questions