pandas: replace string with another string pandas: replace string with another string pandas pandas

pandas: replace string with another string


Solution with replace by dictionary:

df['prod_type'] = df['prod_type'].replace({'respon':'responsive', 'r':'responsive'})print (df)    prod_type0  responsive1  responsive2  responsive3  responsive4  responsive5  responsive6  responsive

If need set all values in column to some string:

df['prod_type'] = 'responsive' 


You don't need to pass regex=True here, as this will look for partial matches, as you''re after exact matches just pass the params as separate args:

In [7]:df['prod_type'] = df['prod_type'].replace('respon' ,'responsvie')df['prod_type'] = df['prod_type'].replace('r', 'responsive')dfOut[7]:    prod_type0  responsive1  responsive2  responsvie3  responsive4  responsvie5  responsive6  responsive


Other solution in case all items from df['prod_type'] will be the same:

df['prod_type'] = ['responsive' for item in df['prod_type']]In[0]: dfOut[0]:prod_type0  responsive1  responsive2  responsive3  responsive4  responsive5  responsive6  responsive