Skip to content Skip to sidebar Skip to footer

Python - How To Load Nested Dictionary Into Pandas Dataframe?

I have a long nested dictionary structured like below, how can I go about loading this into a Pandas dataframe? The sub keys of Feed, Spindle Speed, and Tool remain the same throug

Solution 1:

You were almost there. You have just to reset the multi_index and give the correct column names:

pd.DataFrame.from_dict({(i,j): dictionary[i][j] 
                           foriin dictionary.keys() 
                           forjin dictionary[i].keys()},
                       orient='index').reset_index().rename(
    {'level_0': 'Program', 'level_1': 'Operation Number'}, axis=1)

Solution 2:

Run this line of code after importing pandas to configure your dataframes to not be "sparse":

pd.set_option('display.multi_sparse', False)

From pandas docs:

Option: display.multi_sparse Default: True Function: “Sparsify” MultiIndex display (don’t display repeated elements in outer levels within groups)

Output using first two groupings you provided:

enter image description here

Post a Comment for "Python - How To Load Nested Dictionary Into Pandas Dataframe?"