Reputation: 19
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
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
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