我在python中有以下时间序列表:
list = [(datetime.datetime(2008, 7, 15, 15, 0), 0.134),
(datetime.datetime(2008, 7, 15, 16, 0), 0.0),
(datetime.datetime(2008, 7, 15, 17, 0), 0.0),
(datetime.datetime(2008, 7, 15, 18, 0), 0.0),
(datetime.datetime(2008, 7, 15, 19, 0), 0.0),
(datetime.datetime(2008, 7, 15, 20, 0), 0.0),
(datetime.datetime(2008, 7, 15, 21, 0), 0.0),
(datetime.datetime(2008, 7, 15, 22, 0), 0.0),
(datetime.datetime(2008, 7, 15, 23, 0), 0.0),
(datetime.datetime(2008, 7, 16, 0, 0), 0.0)]
此列表是一个键值对,其中键是datetime,value是以逗号分隔后的值。我想从键(日期时间)和值(十进制值)创建熊猫系列。任何人都可以帮我将上面的时间序列值列表拆分成两个列表(list1和list2),这样我就可以从以下代码中创建pandas Series对象以进一步分析?
import pandas as pd
ts = pd.Series(list1, list2)
答案 0 :(得分:4)
In [34]: pd.Series(*zip(*((b,a) for a,b in data)))
Out[34]:
2008-07-15 15:00:00 0.134
2008-07-15 16:00:00 0.000
2008-07-15 17:00:00 0.000
2008-07-15 18:00:00 0.000
2008-07-15 19:00:00 0.000
2008-07-15 20:00:00 0.000
2008-07-15 21:00:00 0.000
2008-07-15 22:00:00 0.000
2008-07-15 23:00:00 0.000
2008-07-16 00:00:00 0.000
dtype: float64
或者,避免制造单行的疯狂欲望:
dates, vals = zip(*data)
s = pd.Series(vals, index=dates)
如果数据非常长,您可以使用itertools.izip避免创建中间元组:
import itertools as IT
dates, vals = IT.izip(*data)
s = pd.Series(vals, index=dates)
答案 1 :(得分:3)
import pandas as pd
my_list = [(datetime.datetime(2008, 7, 15, 15, 0), 0.134),
(datetime.datetime(2008, 7, 15, 16, 0), 0.0),
(datetime.datetime(2008, 7, 15, 17, 0), 0.0),
(datetime.datetime(2008, 7, 15, 18, 0), 0.0),
(datetime.datetime(2008, 7, 15, 19, 0), 0.0),
(datetime.datetime(2008, 7, 15, 20, 0), 0.0),
(datetime.datetime(2008, 7, 15, 21, 0), 0.0),
(datetime.datetime(2008, 7, 15, 22, 0), 0.0),
(datetime.datetime(2008, 7, 15, 23, 0), 0.0),
(datetime.datetime(2008, 7, 16, 0, 0), 0.0)]
ts = pd.Series(zip(*my_list))
zip(*my_list)
有效地从您的数据中创建了两个元组,一个是datetime
个对象的元组,一个是您的值。然后将这两个作为参数传递给pd.Series
。
答案 2 :(得分:0)
尝试
d = {}
for i in my_list:
d[i[0]] = i[1]
s = pd.series (d)