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