0

I would like to convert nested dict to DataFrame object. I have a dictionary:

{
'David X': {
            'something1': 'string1'
            'something2': 'string2'
            'something3': 'string3'
            },
'David Y': {
            'something1': 'str1'
            'something2': 'str2'
            'something3': 'str3'
            }
}

I would like to obtain DataFrame object with rows consist of names, columns consist of 'something {1,2,3}' and appropriate values.

ltrd
  • 326
  • 1
  • 8
  • Related: [Converting list of dictionaries to pandas DataFrame](https://stackoverflow.com/q/20638006/4909087) – cs95 Mar 02 '19 at 08:52
  • This should be a duplicate one [would be duplicate link](https://stackoverflow.com/questions/13575090/construct-pandas-dataframe-from-items-in-nested-dictionary) – Karn Kumar Mar 02 '19 at 11:08
  • Here is [another duplicate](https://stackoverflow.com/questions/18837262/convert-python-dict-into-a-dataframe) – Karn Kumar Mar 02 '19 at 11:10

1 Answers1

3

Use DataFrame.from_dict:

df = pd.DataFrame.from_dict(d, orient='index')
print (df)
        something1 something2 something3
David X    string1    string2    string3
David Y       str1       str2       str3
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252