简体   繁体   English

将列标题更改为日期格式在 dataframe 中不起作用

[英]Changing column headers to date format not working in dataframe

I have a dataframe where all of the headers are in string format.我有一个 dataframe ,其中所有标题都是字符串格式。 However most of the headers are actually YYYY-MM dates and I would like to change these into date formate.但是大多数标题实际上是 YYYY-MM 日期,我想将它们更改为日期格式。

ldnCrimes.columns: ldnCrimes.columns:

Index(['WardCode', 'Ward Name', 'Borough', 'Major Category', 'Minor Category',
       '2010-04', '2010-05', '2010-06', '2010-07', '2010-08',
       ...
       '2018-03', '2018-04', '2018-05', '2018-06', '2018-07', '2018-08',
       '2018-09', '2018-10', '2018-11', '2018-12'],
      dtype='object', length=110)`

When trying:尝试时:

ldnCrime.columns = ldnCrime.columns[:1].tolist() + pd.to_datetime(ldnCrime.columns[5:]).tolist()

but then I get the following error:但后来我收到以下错误:

ValueError: Length mismatch: Expected axis has 110 elements, new values have 106 elements ValueError:长度不匹配:预期轴有 110 个元素,新值有 106 个元素

The 4 elements are non-date headers and I don't want to change their format.这 4 个元素是非日期标题,我不想更改它们的格式。

but then I get the following error:但后来我收到以下错误:

ValueError: Length mismatch: Expected axis has 110 elements, new values have 106 elements ValueError:长度不匹配:预期轴有 110 个元素,新值有 106 个元素

The 4 elements are non-date headers and I don't want to change their format.这 4 个元素是非日期标题,我不想更改它们的格式。

Thanks in advance any help you may provide.在此先感谢您提供的任何帮助。

Use solution from comment of @Henry Yik - select first 5 values:使用@Henry Yik 评论中的解决方案 - select 前5值:

idx = ['WardCode', 'Ward Name', 'Borough', 'Major Category', 'Minor Category',
       '2010-04', '2010-05', '2010-06', '2010-07', '2010-08',
       '2018-03', '2018-04', '2018-05', '2018-06', '2018-07', '2018-08',
       '2018-09', '2018-10', '2018-11', '2018-12']

ldnCrime = pd.DataFrame(columns=idx)

ldnCrime.columns = ldnCrime.columns[:5].tolist()+pd.to_datetime(ldnCrime.columns[5:]).tolist()

Another idea is convert values to Series and use fillna :另一个想法是将值转换为Series并使用fillna

c = ldnCrime.columns.to_series()
ldnCrime.columns = pd.to_datetime(c, errors='coerce').fillna(c)

print (ldnCrime.columns)
Index([         'WardCode',         'Ward Name',           'Borough',
          'Major Category',    'Minor Category', 2010-04-01 00:00:00,
       2010-05-01 00:00:00, 2010-06-01 00:00:00, 2010-07-01 00:00:00,
       2010-08-01 00:00:00, 2018-03-01 00:00:00, 2018-04-01 00:00:00,
       2018-05-01 00:00:00, 2018-06-01 00:00:00, 2018-07-01 00:00:00,
       2018-08-01 00:00:00, 2018-09-01 00:00:00, 2018-10-01 00:00:00,
       2018-11-01 00:00:00, 2018-12-01 00:00:00],
      dtype='object')

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

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