简体   繁体   English

Python熊猫read_csv之类的功能从列表到DataFrame?

[英]Python pandas read_csv like functionality from list to a DataFrame?

I have a list with values like the following: 我有一个列表,其值如下所示:

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

I know if this were in a csv file, I could read it into a DataFrame with the Date and Timestamps put into index to make the DataFrame a time series. 我知道这是否在csv文件中,我可以将其读入DataFrame中,并将Date和Timestamps放入索引中,以使DataFrame成为时间序列。

How to achieve this without saving the list to a csv file? 如何在不将列表保存到csv文件的情况下实现此目标?

I tried df=pd.DataFrame(tlist, columns=['date_time', 'count']) and then df=df.set_index('date_time') 我尝试了df = pd.DataFrame(tlist,columns = ['date_time','count'])然后是df = df.set_index('date_time')

But the index values are coming out as objects, rather than TimeStamps: 但是索引值是作为对象而不是TimeStamps出现的:

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)
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

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

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