I recently inherited a python file that has a really long dictionary that I would like to convert to a pandas dataframe. I will then take that dataframe and export it to a database with SqlAlchemy to use as a lookup table.
Here is a very small example of what I'm dealing with:
example_dict = { 'Foo': ['A', 'B', 'C', 'D'],
'Buzz': ['E', 'F', 'G', 'H'],
'Fizz': ['I', 'J', 'K'],
'Fuu':['L']}
As you probably noticed, the length of each list is not the same.
I would like to convert to a dataframe that resembles
Col1 Col2
A Foo
B Foo
C Foo
D Foo
E Buzz
F Buzz
G Buzz
.. ..
L Fuu
I've tried using
df = pd.DataFrame(example_dict)
and then using various methods to try and reshape [df.T, df.melt, df.pivot, df.stack, df.unstack]
I also tried
df = pd.DataFrame.from_dict(example_dict, orient='index')
I thought .explode()
might be a good route because the pandas documentation example is very similar to my problem, but I keep running into errors because the arrays are not of equal size.
Any help is greatly appreciated. Thank you!