nivedan gowda
nivedan gowda

Reputation: 205

Creating a new column to get the rupee converted value of different currencies using pandas

i have a column which is of type object with different currencies. I want to create a new column that converts currencies into rupees. I also have a dictionary that have currency conversion details to indian rupees meaning what is the ruppes conversion of 1$ etc (Example- 1$:70, 1€:79,1€:90,1¥:0.654) Dictionary looks like this:- d1 = {'1$':70, '1€':79,'1€':90,'1¥':0.654}

Dataframe looks like this:-

    Currency 
    '20$'    
    '30€'    
    '40€'    
    '35¥'    

I want to get:-

    Currency Rup_convrtd
    '20$'    1400
    '30€'    2700
    '40€'    3600
    '35¥'    22.855

Please somebody help me in getting the Rup_convrtd column using Pandas.

Upvotes: 0

Views: 42

Answers (1)

jezrael
jezrael

Reputation: 863301

Use:

d1 = {'1$':70, '1€':79,'1€':90,'1¥':0.654}
#first remove 1 with indexing
d = {k[1:]:v for k, v in d1.items()}
print (d)
{'$': 70, '€': 90, '¥': 0.654}

#map last value of column by dict and multiple all values without last
df['Rup_convrtd'] = df['Currency'].str[-1].map(d).mul(df['Currency'].str[:-1].astype(int))
print (df)
  Currency  Rup_convrtd
0      20$      1400.00
1      30€      2700.00
2      40€      3600.00
3      35¥        22.89

Upvotes: 1

Related Questions