Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert Dictionary with Differing Lengths to DataFRame

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading