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 a dict that contains a tuple of lists to a Data frame?

I have a dict of clusters in the form:

{1: array([[31.47671257, 30.0206217 ],
    [31.49589   , 30.02547   ],
    [31.474725  , 30.025477  ],
    [31.448643  , 30.018775  ],
    [31.486     , 30.014     ],
    [31.438     , 30.002     ],
    [31.461539  , 30.042498  ,....)
2: array([[31.29888   , 29.989722  ],
    [31.31500756, 29.97498111],
    [31.30848442, 30.00917636],
    [31.35363137, 29.97423761],
    [31.318159  , 29.984137  ],
    [31.317     , 29.979063  ],
    [31.312838  , 29.963003  ],
    ) etc.

I want the output to be represented as a data frame like this one:

     Lng       Lat       no_of_Cluster 
0  31.49589   30.02547       1
1  31.474725  30.025477      4
2  31.486     30.014         7
   

I used this function

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

df = pd.DataFrame.from_dict(Output, orient='index',columns=['A'])

But this is my output

                        A
1   [[31.47671257, 30.0206217], [31.49589, 30.0254...
2   [[31.29888, 29.989722], [31.31500756, 29.97498...
3   [[31.27858, 29.966307], [31.281, 29.975], [31....
4   [[31.62090813, 30.0946129], [31.74450432, 30.1...
5   [[31.577472, 30.159004], [31.62640129, 30.1430...
6   [[31.2357, 30.0444], [31.23887574, 30.0444], [...
7   [[31.339338, 30.103572], [31.33045708, 30.0610...
8   [[31.30796944, 29.86398859], [31.318988, 29.89...
9   [[31.413884509277, 30.114512733553], [31.34002...
10  [[31.395257, 29.978487], [31.386, 30.05], [31....

How do I convert it?

>Solution :

Try the following:

import pandas as pd

clusters = []
for k,v in data.items():
    df = pd.DataFrame(v, columns=['Lng', 'Lat'])
    df['no_of_Cluster'] = k
    clusters.append(df)
total_df = pd.concat(clusters)
print(total_df)

This will result in

         Lng        Lat  no_of_Cluster
0  31.476713  30.020622              1
1  31.495890  30.025470              1
2  31.474725  30.025477              1
3  31.448643  30.018775              1
4  31.486000  30.014000              1
5  31.438000  30.002000              1
6  31.461539  30.042498              1
0  31.298880  29.989722              2
1  31.315008  29.974981              2
2  31.308484  30.009176              2
3  31.353631  29.974238              2
4  31.318159  29.984137              2
5  31.317000  29.979063              2
6  31.312838  29.963003              2
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