How do I get an age in years and date on pandas
Use this solution with custom function, because count it is not easy because leaps years:
from dateutil.relativedelta import relativedelta
def f(end):
r = relativedelta(pd.to_datetime('now'), end)
return '{} years {} days'.format(r.years, r.days)
df['age'] = df["Date-of-birth"].apply(f)
print (df)
Customer_id Date-of-birth age
0 1 1992-07-02 26 years 22 days
1 2 1991-07-03 27 years 21 days
Calculating age from date/time format in python/pandas
Is this what you want ?
(pd.to_datetime('today').year-pd.to_datetime('1956-07-01').year)
Out[83]: 61
How to convert a date to different format in Python to calculate age
now = pd.Timestamp('now')
df['dob'] = pd.to_datetime(df['dob'])
df['age'] = (now - df['dob']).astype('<m8[Y]')
print(df)
covert your str into datetime objects
You can subtract dob from now to obtain timedelta64[ns]. To convert that to years, use astype('
for your last formatting
df['dob'].apply(lambda x: x.strftime('%Y/%m/%d'))
Related Topics
Ssl: Certificate_Verify_Failed With Python3
Could Not Find a Version That Satisfies the Requirement in Python
Removing Punctuations and Spaces in a String Without Using Regex
How to Add a Delay to Message.Delete()
Typing Greek Letters etc. in Plots
Saving Numpy Array to Txt File Row Wise
How to Iterate Over a Timespan After Days, Hours, Weeks and Months
Creating a New Dataframe Column by Comparing Strings of Two Unequal Dataframes
Get Max Value Comparing Multiple Columns and Return Specific Values
Why Is Python No Longer Waiting for Os.System to Finish
Python Divide by Zero Encountered in Log - Logistic Regression
Add Missing Dates to Pandas Dataframe
How to Write 2 Lists of Items in 2 Columns Instead of 2 Arrays
How to Send Smtp Email for Office365 With Python Using Tls/Ssl
Python Turning 2 Dimensional Strings on My List into Floats
How to Rotate Xticks on One Axis of Figure in Matplotlib Without "Getting" the Labels as a List
Accuracy Score Valueerror: Can't Handle Mix of Binary and Continuous Target