替换 pandas 数据框中的部分字符串 [英] replace part of the string in pandas data frame

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

问题描述

我有pandas数据框,在其中我需要用另一个值替换淡谷的一部分

I have pandas data frame in which I need to replace one part of the vale with another value

例如.我有

HF - Antartica
HF - America
HF - Asia

我想替换其中的HF -部分 因此结果将是

out of which I'd like to replace ony the HF - part thus the result would be

Hi Funny Antartica
Hi Funny America
Hi Funny Asia

我已经尝试过pd.replace(),但是它不起作用,因为我只需要替换一部分字符串,而不是整个字符串

I have tried pd.replace() but it doesnt work as I need only one part of the string replaced, rather than the entire string

推荐答案

似乎您需要

It seems you need Series.replace:

print (df)
              val
0  HF - Antartica
1    HF - America
2       HF - Asia

print (df.val.replace({'HF -':'Hi'}, regex=True))
0    Hi Antartica
1      Hi America
2         Hi Asia
Name: val, dtype: object

str.replace :

print (df.val.str.replace('HF -', 'Hi'))
0    Hi Antartica
1      Hi America
2         Hi Asia
Name: val, dtype: object

这篇关于替换 pandas 数据框中的部分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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