Pandas calculating age from a date

2024/10/10 20:17:06

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).

ORIGINAL DATA SET

"JOLIE", 09091959,02051983
"PORTMAN",02111979,01272002
"MOORE", 01281975,01182009
"BEST", 04081973,07022008
"MONROE", 04161957,11231979

LOAD DATA

from pandas import DataFrame, read_csv
import matplotlib.pyplot as plt
import pandas as pdcolumns = ['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_dataIn [1]: df1
Out [1]:lname          dob          scd_csr_mdy0    JOLIE          09091959     020519831    PORTMAN        02111979     012720022    MOORE          01281975     011820093    BEST           04081973     070220084    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'

FINAL OUTCOME

     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

Any suggestions.....?

Answer

Convert the dob column from string to a datetime object

df1['dob'] = pd.to_datetime(df1['dob'])
now = datetime.now()    
df1['age'] = now - df1['dob']
https://en.xdnf.cn/q/118416.html

Related Q&A

Create new folders within multiple existing folders with python

I am looking for a way to create new folders within multiple existing folders. For example I have folders a,b,c.. etc and I want to create a new folder inside each of these existing folders and name th…

extract a column from text file

I have a a text file (huge amount of float numbers) with 25 columns. I want to extract column 14 and divide it by column 15. I could not extract this two columns. Codes:with open(sample for north.txt) …

kivy buildozer Compile Error pythonforandroid.toolchain

Compile platformCommand failed: /usr/bin/python3 -m pythonforandroid.toolchain create --dist_name=main -- bootstrap=sdl2 --requirements=kivy,python3 --arch armeabi- v7a --copy-libs --color=always --…

Django Error: No FlatPage matches the given query

SITE_ID = 1and (r, include(django.contrib.flatpages.urls)), is in urls.py.What can I do to fix this error? Django is still displaying this error - I have googled and I cant find anything.File urls.pyf…

I need to automate the filling of a HTML form in a web browser, how?

I am trying to build a python script that captures my screen (a website will be opened), finds the coordinates of a text entry box on the displayed web site, and then clicks in that text entry box. I a…

Page not found (404) at /user_signup in Django

Getting 404 error on signup and some more URLs. /login, /logout, /admin is working perfectly. Im making a web app that lets a user login, logout, search a flight, book a flight and view the bookings ma…

tensorflow:Your input ran out of data when using custom generator

I am using custom generator to pass my data. But i keep encountering an error which says i have run out of data and to use repeat() when passing the dataset. i am using plain generator therefore it is …

script to get the max from column based on other column values

I need a script to read in a csv file(orig.csv) and output a reformatted csv file(format.csv) The orig csv file will look like this: Time,Label,frame,slot,SSN,Board,BT,SRN,LabelFrame,SRNAME,LabelID,Int…

Selenium code is not able to scrape ofashion.com.cn

I was building a web scraper by using python selenium. The script scraped sites like amazon, stack overflow and flipcart but wasnt able to scrape ofashion. It is always returning me a blank .csv file.H…

How can I access each estimater in scikit-learn pipelines? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…