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.

Nadav Kiani
  • 271
  • 2
  • 4
  • 11

1 Answers1

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) 
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252