Rajesh Rs
Rajesh Rs

Reputation: 1391

Modify column data based on other column in pandas

Im new to python and pandas I have a csv with data which im able to read and extract data using pandas as shown below

data = pd.read_csv("train.csv")

I have many columns below is the columns in question

enter image description here

Here i want to convert the price column into USD equivalent for all the entries. following is the code which i tried

currencyToUSD = {
"USD": 1,
"AUD": 0.7,
"EUR": 1.12,
"HKD": 0.13,
"INR": 0.014,
"KRW": 0.00085
}
for row in data.itertuples():
   data[row.index]['price'] = row.price*currencyToUSD[row.currency]

It throws errors, what i want to achieve is to modify the 'price' column based on the value of 'currency' column. Guide me with the right way to achieve this

Thanks in advance

Upvotes: 1

Views: 37

Answers (1)

jezrael
jezrael

Reputation: 862671

Here itertuples should be omit for improve performance, use Series.map for new Series for multiple column price:

data['price'] = data.price*data.currency.map(currencyToUSD)

Upvotes: 1

Related Questions