简体   繁体   English

纠正不完整的年份 - Python

[英]Rectify incomplete year - Python

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?例如,217 代表 2017 年,17 代表 2017 年等等 是否可以在没有硬编码替换的情况下用正确的年份替换这些值?

I've searched a bit for any solution myself, but haven't been able to land on anything useful.我自己搜索了一些解决方案,但没有找到任何有用的东西。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM