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

How to make a year range column into separate columns?

I am using Python to analyze a data set that has a column with a year range (see below for example):

Name Years Range
Andy 1985 – 1987
Bruce 2011 – 2018

I am trying to convert the "Years Range" column that has a string of start and end years into two separate columns within the data frame to: "Year Start" and "Year End".

Name Years Range Year Start Year End
Andy 1985 – 1987 1985 1987
Bruce 2011 – 2018 2011 2018

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

>Solution :

You can use expand=True within split function

df[['Year Start','Year End']]  = df['Years Range'].str.split('-',expand=True)

output #

    Nmae Years_Range Year Start Year End
0   NAdy   1995-1987       1995     1987
1  bruce   1890-8775       1890     8775
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