0

I'm very noob in python. My dict:

my_dict:{0:['12531','1253145','251231','151315','51555'],
         1:['1551','12554','454545']}

I need to convert this as a DataFrame:

ID      Cluster
12531   0
1253145 0
251231  0
151315  0
51555   0
1551    1
12554   1
454545  1

I tried using

pd.DataFrame.from_dict({(i,j):clusters[i][j]
                        for i in clusters.keys()
                        for j in clusters[i].keys()}
                       ,columns=['Cluster','ID'])

but it is not what I want.

0

2 Answers 2

4

You can generate a Series and explode:

(pd.Series(my_dict)
   .explode()
   .rename_axis('Cluster')
   .reset_index(name='ID')
 )

Output:

   Cluster       ID
0        0    12531
1        0  1253145
2        0   251231
3        0   151315
4        0    51555
5        1     1551
6        1    12554
7        1   454545
Sign up to request clarification or add additional context in comments.

4 Comments

Why 1-Cluster? Whappened if I get from 0 to 5 clusters? It will works?
@Hctor you inverted the 0s and 1s between the dictionary and the end result, thus the query. If this was a mistake, just remove this line. What would be the logic for values 1 to 5?
Sometimes I got clusters from 0 to 5 and I was asking if there is possible for that kind of dicts.
Yes it should work for any key
1

You could modify my_dict to create a list of dictionaries and pass it to the DataFrame constructor:

out = pd.DataFrame([{'ID': v, 'Cluster': 1 - k} 
                    for k, lst in my_dict.items() for v in lst])

Output:

        ID  Cluster
0    12531        1
1  1253145        1
2   251231        1
3   151315        1
4    51555        1
5     1551        0
6    12554        0
7   454545        0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.