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 list
s contain float
values.
+------+-----------+------+
| key1 | key2 | key3 |
+------+-----------+------+
| | a | b | c | |
+------+---+---+---+------+
| 0 | 0 | 0 | 0 | 0 |
+------+---+---+---+------+
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With