这是我的数据
Customer_id Date-of-birth
1 1992-07-02
2 1991-07-03
这是我的代码
import datetime as dt
df['now'] = dt.datetime.now()
df['age'] = df['now'].dt.date - df['Date-of-birth']
这是的结果
Customer_id Date-of-birth age
1 1992-07-02 xxxx days
2 1991-07-03 xxxx days
我想要的结果是
Customer_id Date-of-birth age
1 1992-07-02 26 years 22 days
2 1991-07-03 27 years 21 days
现在让您知道,根据df.dtypes
,Date-of-birth
是一个对象,因为它基于下拉中的客户输入
我怎样才能做到这一点?我希望这个问题足够清楚
将此解决方案与自定义功能一起使用,因为计数并不容易,因为跨越了几年:
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
输入:
import pandas as pd
import datetime as dt
now = dt.datetime.now()
for i in range(0, len(df)):
diff = now - dt.datetime.strptime(df['Date-of-Birth'][i], '%Y-%m-%d')
years = diff.days // 365
days = diff.days - (years*365)
df['age'][i] = str(years) + ' years ' + str(days) + ' days'
print(df)
输出:
Customer_id Date-of-Birth age
1 1992-07-04 26 years 25 days
2 1991-07-04 27 years 26 days
也许您可以使用以下内容。请注意,它依赖于平均每年有365.25
天的事实,因此有时可能会休息一天。
import datetime as dt
def year_days_diff(x):
diff = (dt.datetime.now() - x).days
return str(int(diff / 365.25)) + ' years ' + str(int(diff / 365.25 % 1 * 365.25)) + ' days'
示例:
birth_date = dt.datetime.now() - dt.timedelta(10000)
year_days_diff(birth_date)
输出:
'27 years 138 days'
这可以通过四舍五入到年来确定年龄。
ref_date = dt.datetime.now()
df['age'] = df['Date-of-birth'].apply(lambda x: len(pd.date_range(start = x, end = ref_date, freq = 'Y')))
使用astype('<m8[Y]')
例如:
df['age'] = (pd.to_datetime('now') - df['Date-of-birth']).astype('<m8[Y]')
演示:
import pandas as pd
df = pd.DataFrame({"Date-of-birth": pd.to_datetime(['1992-07-24', '1991-07-24'])})
df["age"] = (pd.to_datetime('now') - df["Date-of-birth"]).astype('<m8[Y]')
print(df)
输出:
Date-of-birth age
0 1992-07-24 25.0
1 1991-07-24 27.0