如何修改索引'date'的代码并创建MultiIndex列如下:
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
部分数据如下:
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'}]
以下是代码:
import json
import pandas as pd
dat = json.load(open('pruItems.json'))
get_cols = lambda x: ['date', x['fund']+'_bid', x['fund']+'_ask']
get_frame = lambda x: pd.DataFrame(x['data'], columns = get_cols(x))
out = []
for d in dat:
try:
out.append(get_frame(d))
except:
pass
out = reduce(lambda x,y: pd.merge(x,y,how='outer'), out)
print out.ix[:5,:5]