Skip to main content
  1. About
  2. For Teams
Asked
Viewed 472 times
2

I have data that looks like this:

Currency    Average Cost for two
0   Botswana Pula(P)    1100
1   Botswana Pula(P)    1200
2   Botswana Pula(P)    4000
3   Botswana Pula(P)    1500
4   Botswana Pula(P)    1500

I want to create a new column that will convert the cost to dollars. Just to mention, there are 12 currencies.

This is what I have written:

for i in range(len(df)) :
if(df[i]['Currency'] == 'Botswana Pula(P)'):
    df[i]['new cost'] = df[i]['Average Cost for two'] * 0.095
if (df[i][['Currency'] == 'Brazilian Real(R$)']):
    df[i]['new cost'] = df[i]['Average Cost for two'] * 0.266
and so on...

With this code, I have got an error.

0

1 Answer 1

3

Create dictionary for all currencies, map column for their value and multiple with Average Cost for two column:

d = {'Botswana Pula(P)':0.095, 'Brazilian Real(R$)':0.266, ...}

df['new cost'] = df['Average Cost for two'] * df['Currency'].map(d) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.