Python 3.x 输出从日期时间索引设置的唯一年份
我有一个日期时间索引,如下所示Python 3.x 输出从日期时间索引设置的唯一年份,python-3.x,pandas,Python 3.x,Pandas,我有一个日期时间索引,如下所示 >>> temp DatetimeIndex(['2017-01-03', '2017-01-04', '2017-01-05', '2017-01-06', '2017-01-09', '2017-01-10', '2017-01-11', '2017-01-12', '2017-01-13', '2017-01-16', ...
>>> temp
DatetimeIndex(['2017-01-03', '2017-01-04', '2017-01-05', '2017-01-06',
'2017-01-09', '2017-01-10', '2017-01-11', '2017-01-12',
'2017-01-13', '2017-01-16',
...
'2017-12-27', '2017-12-28', '2017-12-29', '2018-01-02',
'2018-01-03', '2018-01-04', '2018-01-05', '2018-01-08',
'2018-01-09', '2018-01-10'],
dtype='datetime64[ns]', length=251, freq=None)
>>> temp.year
Int64Index([2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
...
2017, 2017, 2017, 2018, 2018, 2018, 2018, 2018, 2018, 2018],
dtype='int64', length=251)
我想输出唯一的年份,例如作为列表[20172018]。
我尝试了下面的命令,但它并没有像我预期的那样工作
>>> temp.year.drop_duplicates
<bound method Index.drop_duplicates of Int64Index([2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017,
...
2017, 2017, 2017, 2018, 2018, 2018, 2018, 2018, 2018, 2018],
dtype='int64', length=251)>
>>临时年份删除重复项
非常感谢您的建议。您错过了()
,也需要转换为列表:
L = temp.year.drop_duplicates().tolist()
L = list(temp.year.drop_duplicates())
另一个解决方案包括:
L = list(temp.year.unique())
print (L)
[2017, 2018]