Reputation: 460
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
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