如何计算人的年龄(基于dob列)并使用新值向数据框添加列?
数据框如下所示:
lname fname dob
0 DOE LAURIE 03011979
1 BOURNE JASON 06111978
2 GRINCH XMAS 12131988
3 DOE JOHN 11121986
我尝试了以下操作:
now = datetime.now()
df1['age'] = now - df1['dob']
但是,收到以下错误:
TypeError:不支持的操作数类型 - :' datetime.datetime'和' str'
答案 0 :(得分:26)
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)
产量
ssno lname fname pos_title ser gender \
0 23456789 PLILEY JODY BUDG ANAL 560 F
1 987654321 NOEL HEATHER PRTG SRVCS SPECLST 1654 F
2 234567891 SONJU LAURIE SUPVY CONTR SPECLST 1102 F
3 345678912 MANNING CYNTHIA SOC SCNTST 101 F
4 456789123 NAUERTZ ELIZABETH OFF AUTOMATION ASST 326 F
dob age
0 1971-03-18 00:00:00 43
1 1952-12-08 18:00:00 61
2 1999-01-09 00:00:00 15
3 1992-08-16 00:00:00 22
4 1987-03-13 00:00:00 27
dob
列目前似乎是字符串。第一,
使用Timestamps
将其转换为pd.to_datetime
。'%m%d%y'
将最后两位数字转换为年份,但是
不幸的是假设52
意味着2052年。因为那可能不是
希瑟诺埃尔的出生年份,让我们从dob
减去100年
只要dob
大于now
。您可能希望在条件now
中减去几年至df['dob'] < now
,因为与一名1岁的工人相比,他可能稍微有一个101岁的工人...... dob
中减去now
以获取timedelta64[ns]。至
将其转换为年份,请使用astype('<m8[Y]')
或astype('timedelta64[Y]')
。答案 1 :(得分:3)
我找到了更简单的解决方案:
// In code Use share GIF and Video for WhatsApp....
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wam"];
savePath = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:_videourl];
_documentInteractionController.UTI = @"net.whatsapp.movie";
_documentInteractionController.delegate = (id)self;
[_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];
输出:
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)
答案 2 :(得分:2)
首先想到的是你的年龄是两位数,这在这个时代并不是一个不错的选择。无论如何,我会假设像05
这样的所有年份实际上都是1905
。这可能不正确(!)但是提出正确的规则将在很大程度上依赖于您的数据。
from datetime import date
def age(date1, date2):
naive_yrs = date2.year - date1.year
if date1.replace(year=date2.year) > date2:
correction = -1
else:
correction = 0
return naive_yrs + correction
df1['age'] = df1['dob'].map(lambda x: age(date(int('19' + x[-2:]), int(x[:2]), int(x[2:-2])), date.today()))
答案 3 :(得分:1)
# Data setup
df
lname fname dob
0 DOE LAURIE 1979-03-01
1 BOURNE JASON 1978-06-11
2 GRINCH XMAS 1988-12-13
3 DOE JOHN 1986-11-12
# Make sure to parse all datetime columns in advance
df['dob'] = pd.to_datetime(df['dob'], errors='coerce')
如果只希望年龄中的年份部分,请使用@unutbu's solution。 。 。
now = pd.to_datetime('now')
now
# Timestamp('2019-04-14 00:00:43.105892')
(now - df['dob']).astype('<m8[Y]')
0 40.0
1 40.0
2 30.0
3 32.0
Name: dob, dtype: float64
另一种选择是减去年份部分并使用来计算月份差异
(now.year - df['dob'].dt.year) - ((now.month - df['dob'].dt.month) < 0)
0 40
1 40
2 30
3 32
Name: dob, dtype: int64
如果要(几乎)精确的年龄(包括小数部分),请查询total_seconds
并除。
(now - df['dob']).dt.total_seconds() / (60*60*24*365.25)
0 40.120446
1 40.840501
2 30.332630
3 32.418872
Name: dob, dtype: float64
答案 4 :(得分:0)
以下解决方案呢?
import datetime as dt
import numpy as np
import pandas as pd
from dateutil.relativedelta import relativedelta
df1['age'] = [relativedelta(pd.to_datetime('now'), d).years for d in df1['dob']]
答案 5 :(得分:0)
当您尝试从出生日期列中查找当前年份的年龄时,请使用此衬纸
import pandas as pd
df["dob"] = pd.to_datetime(data["dob"])
df["age"] = df["dob"].apply(lambda x : (pd.datetime.now().year - x.year))