pandas 从日期算起年龄

编程入门 行业动态 更新时间:2024-10-26 18:15:56
本文介绍了 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 从日期算起年龄

本文发布于:2023-10-18 11:49:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504152.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算起   年龄   日期   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!