[SOLVED] Remove non-numeric in df column with different datatypes


I have a dataframe like this:

Id  Volume
1   350 L
2   250.0
3   150//
4   250 L

i want to remove the non-numeric in Volume column. The desire output is:

Id  Volume
1   350
2   250
3   150
4   250

I’ve tried to use df['Volume'] = df['Volume'].str.extract('(\d+)', expand=False) but it turns the ‘250.0’ and ‘150//’ value become nan.

I’ve also tried to use df['Volume'] = df['Volume'].str[:3] but it also turns the ‘250.0’ and ‘150//’ value become nan.

I also tried to change the column dtypes to string, but it didn’t work. It’s still in object datatype.


This should work : df['Volumne'] = df['Volume'].str.replace(r'[^0-9.]', '')

Answered By – curiousElectron

Answer Checked By – Marilyn (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *