Alisa
Alisa

Reputation: 13

Pandas with nested JSON objects

I have following dataset:

 {'Result': {
            'j': {'confirmed': true, 'version': '1'},
            'z': {'confirmed': false, 'version': '2'},
            'y': {'confirmed': true, 'version': '3'}
            },
         'D': 'null'
        }

And I need a table with columns : name (values j, z, y goes there) and confirmed (true or false goes there).

The closest thing I have tried is pd.Series(df), but it gives me something like:

j                    {'confirmed': true, 'version': '1'}
z                    {'confirmed': false, 'version': '2'}

How can I achieve only two accurate named columns?

Upvotes: 1

Views: 37

Answers (1)

rahlf23
rahlf23

Reputation: 9019

You can use pd.DataFrame.from_dict() with orient='index', then reset_index() and rename() to set the previous index as column 'name':

pd.DataFrame.from_dict(dataset['Result'], orient='index').reset_index().rename(columns={'index': 'name'})

Yields:

   name confirmed version
0     j      true       1
1     y      true       3
2     z     false       2

Upvotes: 1

Related Questions