Home > database >  Remove non-numeric in df column with different datatypes
Remove non-numeric in df column with different datatypes

Time:03-09

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.

CodePudding user response:

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

  • Related