Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

convert birth date to age pandas

(pd.to_datetime('today').year-pd.to_datetime('1956-07-01').year)

Out[83]: 61
Comment

convert birth date column to age pandas

import datetime as DT
import io
import numpy as np
import pandas as pd

pd.options.mode.chained_assignment = 'warn'

content = '''     ssno        lname         fname    pos_title             ser  gender  dob 
0    23456789    PLILEY     JODY        BUDG ANAL             0560  F      031871 
1    987654321   NOEL       HEATHER     PRTG SRVCS SPECLST    1654  F      120852
2    234567891   SONJU      LAURIE      SUPVY CONTR SPECLST   1102  F      010999
3    345678912   MANNING    CYNTHIA     SOC SCNTST            0101  F      081692
4    456789123   NAUERTZ    ELIZABETH   OFF AUTOMATION ASST   0326  F      031387'''

df = pd.read_csv(io.StringIO(content), sep='s{2,}')
df['dob'] = df['dob'].apply('{:06}'.format)

now = pd.Timestamp('now')
df['dob'] = pd.to_datetime(df['dob'], format='%m%d%y')    # 1
df['dob'] = df['dob'].where(df['dob'] < now, df['dob'] -  np.timedelta64(100, 'Y'))   # 2
df['age'] = (now - df['dob']).astype('<m8[Y]')    # 3
print(df)
Comment

PREVIOUS NEXT
Code Example
Python :: how to save python variables locally 
Python :: Check if all values in list are greater than a certain number 
Python :: opencv python rgb to hsv 
Python :: python run uvicorn 
Python :: what does filename = path(file).stem python 
Python :: python else syntax 
Python :: django email verification 
Python :: how to set background color for a button in tkinter 
Python :: iterating over lines in a file 
Python :: best python programs 
Python :: how to add one to the index of a list 
Python :: python pattern 
Python :: python return double quotes instead of single 
Python :: do while loop python 
Python :: how to flatten list of lists in python 
Python :: fluffy ancake recipe 
Python :: install python cap 
Python :: printing hello world in python 
Python :: python anonymous object 
Python :: adding in python 
Python :: scipy cdf example 
Python :: Python Switch case statement Using classes 
Python :: I**2 python 
Python :: if a or b in python 
Python :: how to give values to all users with discord api python grepper 
Python :: Use in in django while preserving order 
Python :: Installez django-cruds-adminlte 
Python :: python update pip windows 
Shell :: kill all server 5000 mac 
Shell :: ubuntu update chrome browser 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =