Harvey Summers
Harvey Summers

Reputation: 25

Python Pandas replace() not working

I have some fields that have some junk in them from an upstream process. I'm trying to delete '\r\nName: hwowneremail, dtype: object' from a column that has this junk appended to an email address.

report_df['Owner'].replace('\r\nName: hwowneremail, dtype: object',inplace=True)
report_df['Owner'][26]

Output:

'   [email protected]\r\nName: hwowneremail, dtype: object'

I've also tried the following variants w/o success:

replace('Name: hwowneremail, dtype: object', inplace=True)

replace('\\r\\nName: hwowneremail, dtype: object', inplace=True

replace(r'\r\nName: hwowneremail, dtype: object', inplace=True)

replace('\r\nName: hwowneremail, dtype: object', "", inplace=True)

replace(to_value='\r\nName: hwowneremail, dtype: object', value=' ',inplace=True)

replace('\\r\\nName: hwowneremail, dtype: object',regex=True,inplace=True)

Thanks in advance for your insight!

Upvotes: 1

Views: 6001

Answers (3)

Daniel Moraite
Daniel Moraite

Reputation: 516

Or sometimes just make sure there is no white space before or after the character/str you are looking for(expl. ? ):

df.replace(r'\s*\?\s*', np.nan, regex=True) 

or just make sure you specify you are looking for a string:

df.replace(r'\?', np.nan, regex=True)

and for both cases: and don`t forget

regex=True 

Upvotes: 0

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210982

alternatively you can use:

report_df.Owner.str.replace(r'\r\n.*', '')

Upvotes: 0

L33T
L33T

Reputation: 304

As far as I remember, Python Pandas was changed a little bit in replace. You should try passing over a regex keyword argument.

Like so;

report_df['Owner'].replace({'\r\nName: hwowneremail, dtype: object':''},regex=True)

Upvotes: 4

Related Questions