0

我有一个列表,其值如下:

[['2013-04-02 19:42:00.474', '1'],
['2013-04-02 19:42:00.529', '2'],
['2013-04-02 19:42:00.543', '3'],
['2013-04-02 19:42:00.592', '4'],
['2013-04-02 19:42:16.671', '5'],
['2013-04-02 19:42:16.686', '6'],
['2013-04-02 19:42:16.708', '7'],
['2013-04-02 19:42:16.912', '8'],
['2013-04-02 19:42:16.941', '9'],
['2013-04-02 19:42:19.721', '10'],
['2013-04-02 19:42:22.826', '11'],
['2013-04-02 19:42:25.609', '8'],
['2013-04-02 19:42:58.225', '5']]

我知道这是否在 csv 文件中,我可以将其读入 DataFrame,并将 Date 和 Timestamps 放入索引中,以使 DataFrame 成为时间序列。

如何在不将列表保存到 csv 文件的情况下实现这一点?

我试过 df=pd.DataFrame(tlist, columns=['date_time', 'count']) 然后 df=df.set_index('date_time')

但是索引值是以对象的形式出现的,而不是时间戳:

df.index

Index([2013-04-02 19:42:00.474, 2013-04-02 19:42:00.529, 2013-04-02 19:42:00.543, ............], dtype=object)
4

1 回答 1

3
In [40]: df.index = df.index.to_datetime()

In [41]: df.index
Out[41]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-04-02 19:42:00.474000, ..., 2013-04-02 19:42:58.225000]
Length: 13, Freq: None, Timezone: None
于 2013-04-25T05:48:39.803 回答