python - DataFrame from list of dicts of dicts -
i have list
of dicts
each key in dict
contains 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') x in data_list 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}
how can create following or similar pandas.dataframe
?
index1 index2 prop_1 prop_2 602 1111 0 1 2222 0 1 603 1111 0 0 2222 1 1
considering can transform series of dictionaries multiple columns data frame doing .apply(pd.series)
, can stack()
original data frame multiindex series , use .apply(pd.series)
:
df.stack().apply(pd.series) # prop_1 prop_2 # 602 1111 0 1 # 2222 0 1 # 603 1111 0 0 # 2222 1 1
Comments
Post a Comment