I am trying to assign values to rows on a certain column in the DataFrame,
exchange_rates = {'TRY': 3.4155, 'NZD': 1.3683, 'HUF': 254.11, 'RUB': 57.185,
'DKK': 6.1693, 'SEK': 7.9095, 'SGD': 1.337, 'CAD': 1.2087, 'JPY': 107.38,
'AUD': 1.2339, 'HRK': 6.1621, 'CHF': 0.94569, 'ILS': 3.5163, 'THB': 33.1,
'GBP': 0.75678, 'BGN': 1.6217, 'KRW': 1130.6, 'NOK': 7.7192, 'CZK': 21.641,
'ZAR': 12.862, 'PHP': 50.809, 'MYR': 4.1945, 'EUR': 0.82919, 'MXN': 17.693,
'CNY': 6.4615, 'HKD': 7.8076, 'RON': 3.8149, 'INR': 63.784, 'PLN': 3.5269,
'IDR': 13163.0, 'BRL': 3.0892}
unique_currencies = ['CAD', 'CHF', 'CNY', 'EUR', 'JPY', 'GBP', 'SGD']
for cur in unique_currencies:
cur_indices = df[df['currency'] == cur].index.tolist()
for row_index in cur_indices:
df.loc[row_index, 'common_amount'] = df.loc[row_index, 'amount'] / exchange_rates[cur]
So the code gets the indices of rows with a certain currency and then assigns value calculated based on its exchange rate to 'USD' to column common_amount.
I am wondering what is the best way to do this?
UPDATE
an example DataFrame,
currency amount
50 CAD 410.85
51 CAD 1441.68
17625 JPY 2797856.0
17663 JPY 1440.0
16734 CNY 27840.00
54546 CNY 273269.53
17654 GBP 384.0
17655 GBP 526.0
16732 CHF 474.7
16733 CHF 195173.3
df