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

Replace by NaN if string contains digits or symbols

I have a dataframe and I need to identify values that contain numbers or symbols in order to eliminate them. Only letters and spaces are allowed. The size of the dataframe is quite big and what I am trying doesn’t result in anything:

df.NAME=df.NAME.replace(r"(/^[a-zA-Z\s]*$/)",np.nan,regex=True)

Any suggestions?
Thank you

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

>Solution :

If you need to only keep items with letters and spaces only, you need

df['NAME']=df[df['NAME'].str.contains(r"^[a-zA-Z\s]*$", np.nan, regex=True)]

That will keep all those items in NAME column that only contain ASCII letters or/and whitespaces.

To support any Unicode letters, you’d need

df['NAME']=df[df['NAME'].str.contains(r"^(?:[^\W\d_]|\s)*$", np.nan, regex=True)]

where (?:[^\W\d_]|\s) matches either any Unicode letter (together with most diacritics) or a whitespace char.

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