我有以下数据框
prod_type
0 responsive
1 responsive
2 respon
3 r
4 respon
5 r
6 responsive
我想将
respon
和r
替换为responsive
,因此最终的数据帧是 prod_type
0 responsive
1 responsive
2 responsive
3 responsive
4 responsive
5 responsive
6 responsive
我尝试了以下方法,但没有成功:
df['prod_type'] = df['prod_type'].replace({'respon' : 'responsvie'}, regex=True)
df['prod_type'] = df['prod_type'].replace({'r' : 'responsive'}, regex=True)
最佳答案
溶液replace
bydictionary
:
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
如果需要,请将列中的所有值设置为某些值:
df['prod_type'] = 'responsive'
关于python - pandas:用另一个字符串替换字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39602824/