How can I calculate the age of a person (based off the dob column) and add a column to the dataframe with the new value?
dataframe looks like the following:
I found easier solution:
import pandas as pd
from datetime import datetime
from datetime import date
d = {'col0': [1, 2, 6],
'col1': [3, 8, 3],
'col2': ['17.02.1979', '11.11.1993', '01.08.1961']}
df = pd.DataFrame(data=d)
def calculate_age(born):
born = datetime.strptime(born, "%d.%m.%Y").date()
today = date.today()
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
df['age'] = df['col6'].apply(calculate_age)
print(df)
output:
col0 col1 col3 age
0 1 3 17.02.1979 39
1 2 8 11.11.1993 24
2 6 3 01.08.1961 57