How to build a MultiIndex Pandas DataFrame from a nested dictionary with lists

2024/10/18 14:48:24

I have the following dictionary.

d= {'key1': {'sub-key1': ['a','b','c','d','e']},'key2': {'sub-key2': ['1','2','3','5','8','9','10']}}

With the help of this post, I managed to successfully convert this dictionary to a DataFrame.

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

However, my DataFrame takes the following form:

                  0  1  2  3  4     5     6
(key1, sub-key1)  a  b  c  d  e  None  None
(key2, sub-key2)  1  2  3  5  8     9    10

I can work with tuples, as index values, however I think it's better to work with a multilevel DataFrame. Post such as this one have helped me to create it in two steps, however I am struggling to do it in one step (i.e. from the initial creation), as the list within the dictionary as well as the tuples afterwards are adding a level of complication.

Answer

I think you are close, for MultiIndex is possible used MultiIndex.from_tuples method:

d = {(i,j): d[i][j] for i in d.keys() for j in d[i].keys()}mux = pd.MultiIndex.from_tuples(d.keys())
df = pd.DataFrame(list(d.values()), index=mux)
print (df)0  1  2  3  4     5     6
key1 sub-key1  a  b  c  d  e  None  None
key2 sub-key2  1  2  3  5  8     9    10

Thanks, Zero for another solution:

df = pd.DataFrame.from_dict({(i,j): d[i][j] for i in d.keys() for j in d[i].keys()},orient='index')df.index = pd.MultiIndex.from_tuples(df.index)
print (df)0  1  2  3  4     5     6
key1 sub-key1  a  b  c  d  e  None  None
key2 sub-key2  1  2  3  5  8     9    10
https://en.xdnf.cn/q/73132.html

Related Q&A

Functions and if - else in python. Mutliple conditions. Codeacademy

Write a function, shut_down, that takes one parameter (you can use anything you like; in this case, wed use s for string).The shut_down function should return "Shutting down..." when it gets …

ipython up and down arrow strange behaviour

In my installation of ipython I have this strange problem where I cannot reliably move through command history with up and down arrows... a lot of the time it just doesnt work (nothing happens on the k…

Python for loop slows and evenutally hangs

Im totally new to Python (as of half an hour ago) and trying to write a simple script to enumerate users on an SMTP server.The users file is a simple list (one per line) of usernames.The script runs fi…

Jupyter Notebook: Change Data Rate Limit Inside Active Notebook

I have a jupyter notebook where an executed cell gives the following error:IOPub data rate exceeded...I understand this is an option:jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10However, …

How to obtain the training error in svm of Scikit-learn?

My question: How do I obtain the training error in the svm module (SVC class)?I am trying to do a plot of error of the train set and test set against the number of training data used ( or other featur…

Flask-Migrate not detecting tables

I have the following project structure:project/__init__.pyfrom flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migratedb = SQLAlchemy() migrate = Migrate()def creat…

Cannot train a neural network solving XOR mapping

I am trying to implement a simple classifier for the XOR problem in Keras. Here is the code:from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation from keras.optim…

Pyarrow read/write from s3

Is it possible to read and write parquet files from one folder to another folder in s3 without converting into pandas using pyarrow.Here is my code:import pyarrow.parquet as pq import pyarrow as pa imp…

matplotlib draw showing nothing

Im using pythons matplotlib to do some contours using contour and contourf functions. They all work fine when using show, but when I try to use draw() inside a method, I get the matplotlib window but n…

Measure the height of a string in Tkinter Python?

I need the height in pixel of a string in a Tkiner widget. It is the text in a row of a Listbox.I know I can measure the width of a string with tkinter.font.Font.measure. But how can I get the height?…