Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace part of the string in pandas data frame

Tags:

python

pandas

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

like image 596
Mr.Riply Avatar asked Feb 19 '17 19:02

Mr.Riply


People also ask

How do I remove part of a string in a data frame?

To remove characters from columns in Pandas DataFrame, use the replace(~) method. Here, [ab] is regex and matches any character that is a or b .

How do I replace a string in a data frame?

You can replace a string in the pandas DataFrame column by using replace(), str. replace() with lambda functions.

How do I change a character in a column in pandas?

We can replace characters using str. replace() method is basically replacing an existing string or character in a string with a new one. we can replace characters in strings is for the entire dataframe as well as for a particular column.


2 Answers

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 
like image 144
jezrael Avatar answered Sep 20 '22 07:09

jezrael


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 
like image 23
kuriouscoder Avatar answered Sep 22 '22 07:09

kuriouscoder