pandas: replace string with another string
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]: df
Out[0]:
prod_type
0 responsive
1 responsive
2 responsive
3 responsive
4 responsive
5 responsive
6 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')
df
Out[7]:
prod_type
0 responsive
1 responsive
2 responsvie
3 responsive
4 responsvie
5 responsive
6 responsive
alternatively, you can use apply function with lambda syntax
df['prod_type'] = df['prod_type'].apply(lambda x: x.replace('respon', 'responsvie'))
Solution with replace
by dictionary
:
df['prod_type'] = df['prod_type'].replace({'respon':'responsive', 'r':'responsive'})
print (df)
prod_type
0 responsive
1 responsive
2 responsive
3 responsive
4 responsive
5 responsive
6 responsive
If need set all values in column to some string
:
df['prod_type'] = 'responsive'