Menu Close

How to construct Pandas DataFrame from items in nested dictionary with Python?

Sometimes, we want to construct Pandas DataFrame from items in nested dictionary with Python.

In this article, we’ll look at how to construct Pandas DataFrame from items in nested dictionary with Python.

How to construct Pandas DataFrame from items in nested dictionary with Python?

To construct Pandas DataFrame from items in nested dictionary with Python, we can use dictionary comprehension to extract the values from the nested dicts.

And then we call from_dict with the returned dictionary to create a data frame.

For instance, we write

user_dict = {
    12: {
        "Category 1": {"att_1": 1, "att_2": "whatever"},
        "Category 2": {"att_1": 23, "att_2": "another"},
    },
    15: {
        "Category 1": {"att_1": 10, "att_2": "foo"},
        "Category 2": {"att_1": 30, "att_2": "bar"},
    },
}

pd.DataFrame.from_dict(
    {(i, j): user_dict[i][j] for i in user_dict.keys() for j in user_dict[i].keys()},
    orient="index",
)

to call from_dict with a dict that gets the values from the keys and values from each nested dict to create the data frame.

We gets the dict keys with the keys method.

Conclusion

To construct Pandas DataFrame from items in nested dictionary with Python, we can use dictionary comprehension to extract the values from the nested dicts.

And then we call from_dict with the returned dictionary to create a data frame.

Posted in Python, Python Answers