Home > Blockchain >  Change some specific values in a column that satisfies the condition in a dataframe
Change some specific values in a column that satisfies the condition in a dataframe

Time:09-19

df[df.CityLocation.str.contains('Delhi',case=False,na=False)].CityLocation= 'New Delhi'

So I want to change any value containing Delhi | delhi | new Delhi | New delhi | DELHI to 'New Delhi' in the column called 'CityLocation'. And I tried the above shown way but it doesn't reflect in dataframe. What am I missing?

CodePudding user response:

use loc:

df.loc[df.CityLocation.str.contains('Delhi',case=False,na=False),"CityLocation"]= 'New Delhi'

CodePudding user response:

try one those two options:

condition = df['CityLocation'].str.contains('Delhi|Delhi|delhi|new Delhi|New delhi|DELHI',na=False)
df.loc[condition ,"CityLocation"]= 'New Delhi'

#or with np.where
df['CityLocation'] = np.where(condition, 'New Delhi', df['CityLocation'])
  • Related