I have a pandas data frame with a column of 'date_of_birth'. Values take the form 1977-10-24T00:00:00.000Z, for example.
I want to capture the year, so I tried the following:
X['date_of_birth'] = X['date_of_birth'].apply(lambda x: int(str(x)[4:]))
This works if I am guaranteed that the first 4 letters are always integers, but in my dataset this does not work, because some dates are messed up or garbage. Is there a way to tweak my lambda without using regex? If not, how can I write this in regex?
source
share