37

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

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

Mr.Riply
  • 825
  • 1
  • 12
  • 34

3 Answers3

53

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    +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 Feb 22 '21 at 17:44
  • @mikey you probably forgot to use "inplace=True" at the end of your series.replace command – ACan Jul 28 '21 at 18:17
  • In case someone needs to replace across the df not series, try this: df.replace(regex=['HF -'], value='Hi Funny') – Umar Yusuf Sep 19 '21 at 17:10
  • 2
    `str.replace(..., regex=True)` is significantly faster than `Series.replace(..., regex=True)` – Maurício Collaça Dec 12 '21 at 12:04
  • 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.', ''). – mapperx Jan 15 '22 at 07:03
16

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
kuriouscoder
  • 5,394
  • 7
  • 26
  • 40
-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)
  • @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 ' \' . – mapperx Jan 15 '22 at 06:56