Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Data cleaning: regex to replace numbers

I have this dataframe:

p=pd.DataFrame({'text':[2,'string']})

and trying to replace digit 2 by an ‘a’ using this code:

p['text']=p['text'].str.replace('\d+', 'a')

But instead of letter a and get NaN?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What am I doing wrong here?

>Solution :

In your dataframe, the first value of the text column is actually a number, not a string, thus the NaN error when you try to call .str. Just convert it to a string first:

p['text'] = p['text'].astype(str).str.replace('\d+', 'a')

Output:

>>> p
     text
0       a
1  string

(Note that .str.replace is soon going to change the default value of regex from True to False, so you won’t be able to use regular expressions without passing regex=True, e.g. .str.replace('\d+', 'a', regex=True))

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading