Pandas get the age from a date (example: date of birth)

后端 未结 6 1166
夕颜
夕颜 2020-12-01 06:33

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:

6条回答
  •  一生所求
    2020-12-01 07:17

    Use this one liner when you are trying to find the age from date of birth column with current year

    import pandas as pd
    
    df["dob"] = pd.to_datetime(data["dob"])
    
    df["age"] = df["dob"].apply(lambda x : (pd.datetime.now().year - x.year))
    

提交回复
热议问题