I have two lists of city names that look like:
old_city = ['DORCHESTER', 'SOUTH EASTON', 'ASHLEY FALLS']
new_city = ['BOSTON', 'EASTON', 'SHEFFIELD']
In one column of my dataframe, I would like to replace every city that is in old_city with the corresponding city name in new_city. What I am doing is:
data['city'] = data['city'].replace('DORCHESTER','BOSTON')
data['city'] = data['city'].replace('SOUTH EASTON','EASTON')
data['city'] = data['city'].replace('ASHLEY FALLS','SHEFFIELD')
But this is very inefficient given that I have more than a hundred names to replace...
Are there any suggestions on better ways? I have read other similar questions, but none of them seem to have two lists like here...
Thank you very much for the advice.
与
data['city'] = data['city'].replace(dict(zip(old_city,new_city)))
使用data.replace(old_city,new_city)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.