Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create multicolumn Pandas DataFrame from nested dict

I have a dict that looks like this:

{"key1":[0.], "key2":{"a":[0.],"b":[0.],"c":[0.]}, "key3":[0.]}

Is there an elegant way to create a Pandas DataFrame with the nested keys a,b,c as subcolumns?

All the lists contain float values.

+------+-----------+------+
| key1 | key2      | key3 |
+------+-----------+------+
|      | a | b | c |      |
+------+---+---+---+------+
| 0    | 0 | 0 | 0 | 0    |
+------+---+---+---+------+
like image 470
Nick Avatar asked Oct 15 '25 09:10

Nick


1 Answers

This is the long-winded method. Note that the second level of your columns should have a value. I've set it to 0 here where it has not been specified.

d = {"key1":[0.], "key2":{"a":[1.],"b":[2.],"c":[3.]}, "key3":[4.]}

cols, data = [], []
for k, v in d.items():
    if not isinstance(v, dict):
        cols.append((k, 0))
        data.append(v)
    else:
        for k2, v2 in v.items():
            cols.append((k, k2))
            data.append(v2)

df = pd.DataFrame(list(zip(*data)), columns=pd.MultiIndex.from_tuples(cols))

print(df)

  key1 key2           key3
     0    a    b    c    0
0  0.0  1.0  2.0  3.0  4.0
like image 132
jpp Avatar answered Oct 16 '25 22:10

jpp