我有一个list
dicts
,其中dict
中的每个键都包含另一个dict
:
In [256]: data_list
Out[256]:
[{'1111': {'index': 602, 'prop_1': 0, 'prop_2': 1},
'2222': {'index': 602, 'prop_1': 0, 'prop_2': 1}},
{'1111': {'index': 603, 'prop_1': 0, 'prop_2': 0},
'2222': {'index': 603, 'prop_1': 1, 'prop_2': 1}}]
In [257]: index = {i.pop('index') for x in data_list for i in x.values()}
In [258]: df = DataFrame(data_list, index=index)
In [259]: df
Out[259]:
1111 2222
602 {u'prop_1': 0, u'prop_2': 1} {u'prop_1': 0, u'prop_2': 1}
603 {u'prop_1': 0, u'prop_2': 0} {u'prop_1': 1, u'prop_2': 1}
如何创建以下或类似的pandas.DataFrame
?
index1 index2 prop_1 prop_2
602 1111 0 1
2222 0 1
603 1111 0 0
2222 1 1