0

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.

chimmy tata
  • 49
  • 1
  • 5

1 Answers1

0

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

  • this still turns the '250.0' and '150//' value become nan. – chimmy tata Mar 09 '22 at 09:23
  • How did you try to change the datatype, the error might be because the object datatype needs to be converted to string. [link](https://stackoverflow.com/questions/22231592/pandas-change-data-type-of-series-to-string) – curiousElectron Mar 09 '22 at 09:29