Raj Mehta
Raj Mehta

Reputation: 19

Several currency symbols in same column?

How can I insert currency symbol (like $, €..) under 'Price' column, when I have different currencies in the same column?

data = [['Shampoo', 0.60, 'USD'], 
        ['Soap', 0.19, 'EURO'], 
        ['Pen', 0.1, 'JPY'], 
        ]

df = pd.DataFrame(data, columns = ['Stuff', 'Price', 'Currency'])
df

Upvotes: 1

Views: 580

Answers (2)

NYC Coder
NYC Coder

Reputation: 7604

You can do this:

curr = {
    'USD': '$',
    'EURO': '€',
    'JPY': '¥',
}
df = pd.DataFrame(data, columns = ['Stuff', 'Price', 'Currency'])
df['Price'] = df.apply(lambda x: curr[x['Currency']] + ' ' + str(x['Price']), axis=1)
print(df)

     Stuff   Price Currency
0  Shampoo   $ 0.6      USD
1     Soap  € 0.19     EURO
2      Pen   ¥ 0.1      JPY

Upvotes: 0

Shubham Sharma
Shubham Sharma

Reputation: 71687

Create a mapping dictionary that maps each currency to its corresponding symbol then use Series.map to map the values in currency column, then concatenate the mapped column with price column:

mapping = {'USD': '$', 'EURO': '€', 'JPY': '¥'}
df['Price'] = df['Currency'].map(mapping) + df['Price'].astype(str)

# print(df)
     Stuff  Price Currency
0  Shampoo   $0.6      USD
1     Soap  €0.19     EURO
2      Pen   ¥0.1      JPY

Upvotes: 1

Related Questions