I have a DataFrame that looks like this:
df = pd.DataFrame({
'name': ['John','Mary', 'Phil', 'Sue', 'Robert', 'Lucy', 'Blake'],
'age': ['15', '20s', 37, 'teen', '', 'elderly', 57]
})
df
name age
0 John 15
1 Mary 20s
2 Phil 37
3 Sue teen
4 Robert
5 Lucy elderly
6 Blake 57
I would like to:
- convert the
age
column into integers (where there is an integer already, or where one is able to be deduced, e.g. from a string) - otherwise replace with
NaN
Here is what I'm looking to get:
name age
0 John 15 <--- was originally a string
1 Mary NaN
2 Phil 37
3 Sue NaN
4 Robert NaN
5 Lucy NaN
6 Blake 57
How would I do this?