Convert Dictionary with Differing Lengths to DataFRame

Question:

I’m trying to convert a dictionary to a DataFrame in python, but the other answers on stack are for slightly different purposes and I can’t seem to do it.

have <- {0: [1, 2], 1: [1]}
want <- ['cluster' = [0, 0, 1], 'value' = [1, 2, 1]]
Asked By: spazznolo

||

Answers:

Try this:

cluster = []
value = []
for i,j in my_dict.items():
    for k in j:
        cluster.append(i)
        value.append(k)

Output

cluster # [0, 0, 1]
value # [1, 2, 1]
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.