f ={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,}
f
Out[12]:{'2012-06-08':388,'2012-06-09':388,'2012-06-10':388,'2012-06-11':389,'2012-06-12':389,'2012-06-13':389,}
pd.DataFrame(f.items(), columns=['Date','DateValue'])
Out[13]:
Date DateValue
02012-06-0838812012-06-0938822012-06-1038832012-06-1138942012-06-1238952012-06-13389
2、DataFrame转字典
1、结合groupby
>>> ptest = pd.DataFrame([['a',1],['a',2],['b',3]], columns=['id','value'])>>> ptest
id value
0 a 11 a 22 b 3>>>{k: g["value"].tolist()for k,g in ptest.groupby("id")}{'a':[1,2],'b':[3]}2、某一列为key,某一列为value
>>> df = pd.DataFrame({'name':[1,2,3],"class":[11,22,33],"price":[111,222,333]})>>> df
name classprice011111112222222333333>>> df.set_index('name')['class'].to_dict(){1:11,2:22,3:33}>>> df.to_dict(orient="dict")# 参数 orient : str {‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, ‘index’}{'class':{0:11,1:22,2:33},'name':{0:1,1:2,2:3},'price':{0:111,1:222,2:333}}>>> df.to_dict(orient="list"){'class':[11,22,33],'name':[1,2,3],'price':[111,222,333]}>>> df.to_dict(orient="series"){'class':011122233
Name:class, dtype: int64,'name':011223
Name: name, dtype: int64,'price':011112222333
Name: price, dtype: int64}>>> df.to_dict(orient="split"){'index':[0,1,2],'columns':['class','name','price'],'data':[[11,1,111],[22,2,222],[33,3,333]]}>>> df.to_dict(orient="records")[{'class':11,'name':1,'price':111},{'class':22,'name':2,'price':222},{'class':33,'name':3,'price':333}]>>> df.to_dict(orient="index"){0:{'class':11,'name':1,'price':111},1:{'class':22,'name':2,'price':222},2:{'class':33,'name':3,'price':333}}