0

I have a dataframe data that looks this this:

    age
0   .
1   1.2
2   9
3   .

I want to convert all the rows that have . to an empty value (or np.nan).
My desired output would be:

    age
0   
1   1.2
2   9
3   

I tried two different regular expressions which both didn't work:

data['age'] = data['age'].str.replace(r'^(.)$', '')
data['age'] = data['age'].str.replace(r'\D', '')

Both of the results:

    age
0   
1   NaN
2   NaN
3   
shsh
  • 684
  • 1
  • 7
  • 18
  • What is meant by empty ? This `^[^\d]*(?:\.[^\d]*)+$` ? Which would only partially validate Not a Number. –  Oct 30 '20 at 21:14
  • `str.replace(r'^\.$', '')`. A literal dot must be escaped in a regex. – Wiktor Stribiżew Oct 30 '20 at 21:15
  • In its simplest form, if it doesn't match this `^(?:\s*\d+\s*(?:\s*\.\s*\d*)?|\s*\.\s*\d+\s*)$` it's not a number. –  Oct 30 '20 at 21:20

0 Answers0