pandas :用另一个字符串替换字符串 [英] pandas: replace string with another string

查看:55
本文介绍了 pandas :用另一个字符串替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框

    prod_type
0   responsive
1   responsive
2   respon
3   r
4   respon
5   r
6   responsive

我想用responsive替换responr,所以最终的数据帧是

I would like to replace respon and r with responsive, so the final data frame is

    prod_type
0   responsive
1   responsive
2   responsive
3   responsive
4   responsive
5   responsive
6   responsive

我尝试了以下操作,但没有用:

I tried the following but it did not work:

df['prod_type'] = df['prod_type'].replace({'respon' : 'responsvie'}, regex=True)
df['prod_type'] = df['prod_type'].replace({'r' : 'responsive'}, regex=True)

推荐答案

使用 replace 通过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

如果需要将列中的所有值设置为某些string:

If need set all values in column to some string:

df['prod_type'] = 'responsive' 

这篇关于 pandas :用另一个字符串替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆