'How to convert column currency(USD and CAD) into one currency?
I have one column containing "currency(USD and CAD)" and the other one is the currency value.
How to create a new column that contains converted to USD values?
data = {'currency':['CAD', 'USD', 'CAD', 'USD'],'value':[20, 21, 19, 18]}
df = pd.DataFrame(data)
Solution 1:[1]
Create a mapping dict between USD and other currencies:
rates = {'CAD': 0.7849, 'EUR': 1.1322}
data = {'currency': ['CAD', 'USD', 'CAD', 'EUR'],
'value': [20, 21, 19, 18]}
df = pd.DataFrame(data)
df['USD'] = df['currency'].map(rates).fillna(1) * df['value']
print(df)
# Output
currency value USD
0 CAD 20 15.6980
1 USD 21 21.0000
2 CAD 19 14.9131
3 EUR 18 20.3796
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Corralien |
