I am trying to remove alpha characters and special characters(,) from the column values. When trying to remove the alpha characters it gives NaN as output.
Input Data:
col2
2565.0
23899
876.44
1765.7
3,253.0CA
9876.9B
Output Data:
col2
2565.0
23899
876.44
1765.7
3253.0
9876.9
Code i have been using:
df['col2'] = df['col2'].str.replace(r"[a-zA-Z]",'')
df['col2']=df['col2'].fillna('').str.replace(',',"").astype(float)
Please suggest how to resolve this.
Use Series.replace
and regex which matches "not numbers and dot"
df['col2'] = df.col2.replace('[^\d.]', '', regex=True).astype(float)
Output
col2
0 2565.00
1 23899.00
2 876.44
3 1765.70
4 3253.00
5 9876.90
Use Series.str.replace
:
df['col2'] = df['col2'].str.replace(r'[a-zA-Z,]','', regex=True).astype(float)
print (df)
col2
0 2565.00
1 23899.00
2 876.44
3 1765.70
4 3253.00
5 9876.90
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.