Convert Python dictionary of dictionary of dictionary to table

I am working on python panda. I have nested dictionaries as below.

import pandas as pd

data = {
    "block1": {
        "text1": {
            "key1": "value1",
            "key2": "value2",
        },
        "text2": {
            "key1": "value3",
            "key2": "value4",
        },
    },
    "block2": {
        "text1": {
            "key1": "value5",
            "key2": "value6",
        },
        "text2": {
            "key1": "value7",
            "key2": "value8",
        },
    },
}

df = pd.DataFrame(data)

Could you please help me to create a table in pandas, like this ?

---------------------------------------
|       |  text1        |    text2    |
|       |------------------------------
| Block | key1  | key 2 | Key1 | key2 |
---------------------------------------
| block1|value1 |value2 |value3|value4|
---------------------------------------
| block2|value5 |value6 |value7|value8|
---------------------------------------

  • 1

    Please provide MRE that demonstrates the issue you’re having with your code

    – 




Here is one way to do it with Pandas T, json_normalize, concat, and MultiIndex.from_product:

import pandas as pd

df = df.T

new_df = pd.concat(
    [pd.json_normalize(df[col]) for col in df.columns], axis=1
).set_index(df.index)

new_df.columns = pd.MultiIndex.from_product([df.columns, ["key1", "key2"]])

Then:

print(new_df)
# Output

         text1           text2        
          key1    key2    key1    key2
block1  value1  value2  value3  value4
block2  value5  value6  value7  value8

You can use pd.json_normalize:

df = pd.json_normalize(data)
df.columns = pd.MultiIndex.from_arrays(zip(*df.columns.str.split('.')),
                                       names=['block', None, None])

df = df.stack(level=0, future_stack=True).droplevel(level=0).reset_index()

Output:

>>> out
    block   text1           text2        
             key1    key2    key1    key2
0  block1  value1  value2  value3  value4
1  block2  value5  value6  value7  value8

Minimal Working Example:

data = {'block1': {'text1': {'key1': 'value1', 'key2': 'value2'},
                   'text2': {'key1': 'value3', 'key2': 'value4'}},
        'block2': {'text1': {'key1': 'value5', 'key2': 'value6'},
                   'text2': {'key1': 'value7', 'key2': 'value8'}}}

Leave a Comment