Skip to content
Advertisement

Complex nested dict to pandas with multilevel index

I have a complex nested dictionary with multiple levels. I wish to create a multilevel index pandas dataframe from the dict.

The dict takes the format:

JavaScript

Basically, there are 3 arrays, each containing dicts. In the outer most level, bucket_0, we have X number of foo dicts. In the next level, bucket_1, we have Y number of bar dicts. In the last level, bucket_2, we have Z number of baz dicts.

I know I can iterate thru using 3 for loops, but that’s obviously not desirable. I’m aware of pandas json_normalize() method but am unsure how to use this effectively, especially when trying to create a multilevel index.

My desired result would be a multilevel index dataframe where a row would look like: ['foo', 'bar', 'baz', 'max.value']

Also tagged as Elasticsearch since this is a result from a nested aggregation query.

Advertisement

Answer

You can systematically expand the structure and build the index

JavaScript

output

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement