Nested dictionary to multiindex dataframe where dictionary keys are column labels Nested dictionary to multiindex dataframe where dictionary keys are column labels python python

Nested dictionary to multiindex dataframe where dictionary keys are column labels


Pandas wants the MultiIndex values as tuples, not nested dicts. The simplest thing is to convert your dictionary to the right format before trying to pass it to DataFrame:

>>> reform = {(outerKey, innerKey): values for outerKey, innerDict in dictionary.iteritems() for innerKey, values in innerDict.iteritems()}>>> reform{('A', 'a'): [1, 2, 3, 4, 5], ('A', 'b'): [6, 7, 8, 9, 1], ('B', 'a'): [2, 3, 4, 5, 6], ('B', 'b'): [7, 8, 9, 1, 2]}>>> pandas.DataFrame(reform)   A     B      a  b  a  b0  1  6  2  71  2  7  3  82  3  8  4  93  4  9  5  14  5  1  6  2[5 rows x 4 columns]


dict_of_df = {k: pd.DataFrame(v) for k,v in dictionary.items()}df = pd.concat(dict_of_df, axis=1)

Note that the order of columns is lost for python < 3.6


This answer is a little late to the game, but...

You're looking for the functionality in .stack:

df = pandas.DataFrame.from_dict(dictionary, orient="index").stack().to_frame()# to break out the lists into columnsdf = pd.DataFrame(df[0].values.tolist(), index=df.index)