本文介绍了 pandas 从日期算起年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我真的需要这个帮助.我以前的帖子非常糟糕,不清楚-很抱歉-我希望我可以删除,但希望这会更好.
I really need help with this one. My previous post was very bad and unclear - I'm sorry - I wish I could delete but hopefully this one will be better.
我需要根据日期计算年龄(请参阅分析"部分和最终结果"部分.)
I need to calculate the age based off of a date (see ANALYZE section and FINAL OUTCOME SECTION).
"JOLIE", 09091959,02051983 "PORTMAN",02111979,01272002 "MOORE", 01281975,01182009 "BEST", 04081973,07022008 "MONROE", 04161957,11231979加载数据
from pandas import DataFrame, read_csv import matplotlib.pyplot as plt import pandas as pd columns = ['lname','dob','scd_csr_mdy'] raw_data = pd.read_csv(r'C:\Users\davidlopez\Desktop\Folders\Standard Reports\HR Reports\eeprofil \eeprofil.txt',` names=columns, parse_dates = ['dob','scd_csr_mdy'}) df1 = raw_data In [1]: df1 Out [1]: lname dob scd_csr_mdy 0 JOLIE 09091959 02051983 1 PORTMAN 02111979 01272002 2 MOORE 01281975 01182009 3 BEST 04081973 07022008 4 MONROE 04161957 11231979分析
我尝试执行以下操作,但收到错误:
ANALYZE
I tried doing the following but received an error:
now = datetime.now() df1['age'] = now - df1['dob']但是我收到了错误消息:
But I received the the error:
TypeError: unsported operant type(S) for -: 'datetime.datetime' and 'str'最终结果
lname dob scd_csr_mdy DOB_AGE SCD_AGE 0 JOLIE 09091959 02051983 55 32 1 PORTMAN 02111979 01272002 36 13 2 MOORE 01281975 01182009 40 6 3 BEST 04081973 07022008 42 6 4 MONROE 04161957 11231979 58 35任何建议.....?
推荐答案使用格式将字符串转换为日期时间
Convert string to datetime with format
df1['age'] = now - datetime.strptime(df1['dob'], "%m%d%Y")更多推荐
pandas 从日期算起年龄
发布评论