Python 替换熊猫数据框中的部分字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42331992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:34:57  来源:igfitidea点击:

replace part of the string in pandas data frame

pythonpandas

提问by Mr.Riply

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

我有熊猫数据框,我需要用另一个值替换谷的一部分

for Example. I have

例如。我有

HF - Antartica
HF - America
HF - Asia

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

我想更换其中的任何HF -部分,因此结果是

Hi Funny Antartica
Hi Funny America
Hi Funny Asia

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

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

回答by jezrael

It seems you need Series.replace:

看来你需要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

Similar solution with str.replace:

类似的解决方案str.replace

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

回答by kuriouscoder

To add to @jezrael's answer, you need to include regex=Trueotherwise it would match directly. Also, here it replaces the values across all columns in the data frame. If you don't intend this, you could filter to a column and then replace. For replacing across all values in the data frame, try:

要添加到@jezrael 的答案中,您需要包括regex=True否则它将直接匹配。此外,这里它替换了数据框中所有列的值。如果您不打算这样做,您可以过滤到一列然后替换。要替换数据框中的所有值,请尝试:

df.replace('HF', 'Hi Funny', regex=True)

df.replace('HF', 'Hi Funny', regex=True)

You could also provide a list based patterns and replacement values. The complete set of options are provided in the documentation here.

您还可以提供基于列表的模式和替换值。此处的文档中提供了完整的选项集。

So if the data frame is:

所以如果数据框是:

>df = pd.DataFrame({'Column': ['HF - Antartica', 'HF - America', 'HF - Asia']})
>df.replace('HF', 'Hi Funny', regex=True)

should print:

应该打印:

                 Column
0  Hi Funny - Antartica
1    Hi Funny - America
2       Hi Funny - Asia

回答by Sarmad Mahar

I would like to share one more thing that is very much important, you can replace full stop with space ". " with "." normal full stop

我想再分享一件非常重要的事情,你可以用空格“.”代替句号“.”。正常句号

df['label']=df.label.replace({"\. ": "."},regex=True)