简体   繁体   中英

How do you remove all the alphabetic characters from the string except the numerical characters? tried all the present answers

I have two types of data in my column. I want to remove all the alphabetical characters and keep the digits. I have tried all the regex solution present on SO.

df['numbers']=df['numbers'].str.replace('(/d+)','')

df['numbers']=df['numbers'].str.replace('[A-Z]+','')

and similar ways to remove the alphabets.

Data:

numbers
1N
5
5E
7
8E

Expected output:

1
5
5
7
8

Actual outptut:

numbers
1
nan
5
nan
8

This problem is because, alphanumerics are strings while the numbers are integers.

df['number'] = df['number'].astype('str')
df['number'] = df['number'].str.replace('[A-Z]+','',regex=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM