Reputation: 1293
I need to change '"Saint Louis" to "St. Louis" in this dataframe. These are just a few values, but there are many more:
shoot[shoot['city'] == 'Saint Louis'].iloc[:5,0:3]
Incident Date state city
119 24-Aug-17 Missouri Saint Louis
139 13-May-19 Missouri Saint Louis
217 1-Jun-17 Missouri Saint Louis
220 18-Jun-17 Missouri Saint Louis
228 25-Apr-18 Missouri Saint Louis
I tried this way but I get an error:
shoot.loc[shoot.city == 'Saint Louis', :]['city'] = 'St. Louis'
__main__:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
That message is saying to do like this, but of course that's not practical with so many:
shoot.iloc[119,2] = 'St. Louis'
So, how to do it? I looked at some other solutions but they didn't work for me.
Upvotes: 0
Views: 64
Reputation: 1298
Using apply method :
Incident Date state city
0 119 24-Aug-17 Missouri Saint Louis
1 139 13-May-19 Missouri Saint Louis
2 139 13-May-19 Missouri city1
Code :
df['city'] = df.apply(lambda x: ['St. Louis' if x == 'Saint Louis' else x for x in df['city']])
Result :
Incident Date state city
0 119 24-Aug-17 Missouri St. Louis
1 139 13-May-19 Missouri St. Louis
2 139 13-May-19 Missouri city1
Upvotes: 1