0

I have a pandas series which contains float, integer and alphanumerical elements. The float and integer elements are also of type string like below:-

sr = pd.Series(['80', '25.1', '08!shashi123', '2!shekhar45@!#', '6.23']) 

I want to convert this series into just a numerical series like below and replace the alphanumerical elements with a NaN like below:-

sr = pd.Series([80, 25.1, 'NaN', 'NaN', 6.23])
Shashishekhar Hasabnis
  • 1,636
  • 1
  • 15
  • 36

1 Answers1

3

Use to_numeric with error coercion:

import pandas as pd

sr = pd.Series(['80', '25.1', '08!shashi123', '2!shekhar45@!#', '6.23'])

result = pd.to_numeric(sr, errors='coerce')

print(result)

Output

0    80.00
1    25.10
2      NaN
3      NaN
4     6.23
dtype: float64
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76