Reputation: 786
I have a year column in a dataframe that has unique values like
['2017', '2018', '2019', '2015', '2016', '2011', '2010', '2014',
'215', '2013', nan, '216', '217', '2008', '218', '219',
'2012', '211', '2002', '214', '17']
Some years have been incompletely written. For example, 217 represents 2017, 17 represents 2017 etc Is it possible to replace the values with the correct year without hard coded replacement?
I've searched a bit for any solution myself, but haven't been able to land on anything useful.
Upvotes: 2
Views: 37
Reputation: 14191
import numpy as np
years = ['2017', '2018', '2019', '2015', '2016', '2011', '2010', '2014',
'215', '2013', np.nan, '216', '217', '2008', '218', '219',
'2012', '211', '2002', '214', '17']
corrected_years = ["20" + year[-2:] if year is not np.nan else year for year in years]
Output:
['2017', '2018', '2019', '2015', '2016', '2011', '2010', '2014', '2015', '2013', nan, '2016', '2017', '2008', '2018', '2019', '2012', '2011', '2002', '2014', '2017']
Upvotes: 2