假设有以下字典
d =
{u'2012-06-08': 388,
u'2012-06-09': 388,
u'2012-06-10': 388,
u'2012-06-11': 389,
u'2012-06-12': 389,
u'2012-06-13': 389}
目的转化为下方的dataframe
Date DateValue 0 2012-07-01 391 1 2012-07-02 392 2 2012-07-03 392 ...方法1:从字典中获取键值对(items)
pd.DataFrame(d.items())
Out[1]:
0 1
0 2012-07-02 392
1 2012-07-06 392
2 2012-06-29 391
方法2:利用Series构造器
In [2]: s = pd.Series(d, name='DateValue') # key为index,name设置value名称
Out[2]:
2012-06-08 388
2012-06-09 388
2012-06-10 388
In [3]: s.index.name = 'Date' # 修改index名称
In [4]: s.reset_index() #重置索引
Out[4]:
Date DateValue
0 2012-06-08 388
1 2012-06-09 388
2 2012-06-10 388
参考:https://stackoverflow.com/questions/18837262/convert-python-dict-into-a-dataframe



