Convert a dictionary to dataframe

Question:

I have a dictionary as

 my_dict = {(('KBDL', 'KIAH'), ('KBDL', 'KCVG')): 0, 
 (('KSCK', 'KAFW'), ('KSCK', 'KAFW')): 1}

I want to convert it to a dataframe. I use pd.DataFrame.from_dict(my_dict, orient='index'). However, I get this error: ValueError: Index data must be 1-dimensional.

When I change my dictionary to

 my_dict= {(('KBDL', 'KIAH'), ('KBDL', 'KCVG'), ('KCVG', 'KLAL')): 0, 
 (('KSCK', 'KAFW'), ('KSCK', 'KAFW')): 1}

It works fine.

The desired output I am looking for is a dataframe as

(('KBDL', 'KIAH'), ('KBDL', 'KCVG'))    0 
(('KSCK', 'KAFW'), ('KSCK', 'KAFW'))    1
Asked By: user2512443

||

Answers:

Your way running completely fine.

You can try this way too.

my_dict = {(('KBDL', 'KIAH'), ('KBDL', 'KCVG')): 0, 
 (('KSCK', 'KAFW'), ('KSCK', 'KAFW')): 1}

import pandas as pd

df = pd.DataFrame(my_dict.items())

print(df)

Output

                              0  1
0  ((KBDL, KIAH), (KBDL, KCVG))  0
1  ((KSCK, KAFW), (KSCK, KAFW))  1
Answered By: codester_09