Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to subtract from the date a year specified in another column in python?

Today I have confronted some challenges.

This is an example dataset:

example = {
"a": ['1/1/1954 14:14','2/14/2001 2:00' , '2/15/2002 12:00'],
"b": [1936,1996,1960],
}

#load into df:
example = pd.DataFrame(example)

print(example) 

What I was trying to do is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

example['c'] = example['a'] - example['b']

However, I got the issue:

unsupported operand type(s) for -: 'str' and 'int'

I tried to convert the string to the integer, but it did not work.

Could you please recommend me some package or a method to deal with this subtraction? I have heard about datetime, but I am not sure how to set the dates from column "a" accordingly.

Thank you in advance!

>Solution :

Convert values to datetimes and extract years:

y = pd.to_datetime(example['a']).dt.year
example['c'] = y - example['b']

Or extract integers with length 4 between / and space:

y = example['a'].str.extract(r'/(\d{4})\s+', expand=False).astype(int)
example['c'] = y - example['b']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading