Cyb3rGh0st
Cyb3rGh0st

Reputation: 5

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)

Upvotes: 0

Views: 324

Answers (1)

Corralien
Corralien

Reputation: 120509

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

Upvotes: 1

Related Questions