Reputation: 315
I have Pandas dataframe and want to substitute a lot of values on specific column of df.
How to implement the code below without loop?
for i in range(len(data.loc[:, 'CityID'])):
if data.loc[:, 'CityID'][i] == 1:
data.loc[:, 'CityID'][i] = 1
elif data.loc[:, 'CityID'][i] in (2, 3, 4, 21):
data.loc[:, 'CityID'][i] = 2
elif data.loc[:, 'CityID'][i] in (33, 34):
data.loc[:, 'CityID'][i] = 4
else:
data.loc[:, 'CityID'][i] = 3
Upvotes: 2
Views: 937
Reputation: 164663
You can use a dictionary with pd.Series.map
:
d = {1: 1, 2: 2, 3: 2, 4: 2, 21: 2, 33: 4, 34: 4}
data['CityID'] = data['CityID'].map(d).fillna(3)
If the dictionary construction feels laborious, you can use unpacking:
d = {1: 1, **dict.fromkeys((2, 3, 4, 21), 2), **dict.fromkeys((33, 34), 4}
Upvotes: 4
Reputation: 862611
Use:
m1 = data['CityID'] == 1
m2 = data['CityID'].isin([2, 3, 4, 21])
m3 = data['CityID'].isin([33, 34])
data['CityID'] = np.select([m1, m2, m3], [1,2,4], default=3)
Upvotes: 4