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]]
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]
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]]
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]