Skip to content
Advertisement

Creating a dataframe from a dict where keys are tuples

I have the following dict, with keys as tuples:

JavaScript

I’d like to create a dataframe with 3 columns: Col1, Col2 and Col3 which should look like this:

JavaScript

I can’t figure out how to split the tuples other than parsing the dict pair by pair.

Advertisement

Answer

Construct a Series first, then resetting the index will give you a DataFrame:

JavaScript

You can rename columns afterwards:

JavaScript

Or in one-line, first naming the MultiIndex:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement