1

如何将 Dictionary 导入 DataFrame,并将第一列指定为日期并指定标题:

db = {u'data': [[u'18/06/2013', u'34.8400', u'34.8400'],
  [u'17/06/2013', u'34.4900', u'34.4900']],
  u'fund': u'TGC'},
{u'data': [[u'18/06/2013', u'14.9179', u'14.9179'],
 [u'17/06/2013', u'14.8712', u'14.8712']],
 u'fund': u'FEF'},
{u'data': [[u'18/06/2013', u'6.6780', u'6.6780'],
 [u'17/06/2013', u'6.6510', u'6.6570']],
 u'fund': u'FAF'}]

如下数据框:

                 TGC             FEF            FAF
   date      bid     ask     bid     ask     bid    ask
18/06/2013 34.8400 34.8400 14.9179 14.9179 6.6780 6.6780
17/06/2013 34.4900 34.4900 14.8712 14.8712 6.6510 6.6570
4

1 回答 1

3
import json
import pandas as pd

db = json.loads(open('pruItems.json', 'r').read())
pieces = []
for d in db:
    if d['data']:
        df = pd.DataFrame(d['data'])
        df.columns = ['date', 'bid', 'ask']
        df = df.set_index('date')
        pieces.append(df)

df = pd.concat(pieces, axis=1, keys=[d['fund'] for d in db])

print df

输出:

                TGC               FEF              FAF        
                bid      ask      bid      ask     bid     ask
date                                                          
18/06/2013  34.8400  34.8400  14.9179  14.9179  6.6780  6.6780
17/06/2013  34.4900  34.4900  14.8712  14.8712  6.6510  6.6570
于 2013-06-21T08:40:53.033 回答