[英]replace part of the string in pandas data frame
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()
但它不起作用,因为我只需要替换字符串的一部分,而不是整个字符串
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
To add to @jezrael's answer, you need to include regex=True
otherwise it would match directly.要添加到@jezrael 的答案中,您需要包含
regex=True
否则它将直接匹配。 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:
要替换数据框中的所有值,请尝试:
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
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.