Home > Enterprise >  Create a Dataframe from a dictionary in python
Create a Dataframe from a dictionary in python

Time:03-09

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.

CodePudding user response:

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

CodePudding user response:

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
  • Related