40

I have a pandas data frame in which I need to replace one part of the string value with another string 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 should be:

Hi Funny Antartica
Hi Funny America
Hi Funny Asia

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

1
  • 2
    can you show the dataframe declaration? Commented Feb 19, 2017 at 19:46

3 Answers 3

60

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

Similar solution with str.replace:

print (df.val.str.replace('HF -', 'Hi'))
0    Hi Antartica
1      Hi America
2         Hi Asia
Name: val, dtype: object
Sign up to request clarification or add additional context in comments.

6 Comments

+1 The series.replace solution did not make the replacement in my dataframe with either regex=True or with regex=False. I don't know why, but when I used str.replace, it worked
@mikey you probably forgot to use "inplace=True" at the end of your series.replace command
In case someone needs to replace across the df not series, try this: df.replace(regex=['HF -'], value='Hi Funny')
str.replace(..., regex=True) is significantly faster than Series.replace(..., regex=True)
Useful answer, wanted to do some replacement in a dataframe column for the following string '. St.'. Had to add the \ in other for the full stop to be escaped ->> df.column_1.str.replace('\. St.', '').
|
19

To add to @jezrael's answer, you need to include regex=True otherwise 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:

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

Comments

-2

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)

1 Comment

@Samad I can take a point from your answer. That is to replace a full stop, it needs to be escaped when using regular expressions using ' \' .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.