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

Scatter plotting geographical coordinates

I have a column Loc in my dataframe that contains coordinates I want to plot
I transform it into an array with

z =pd.DataFrame(df.Loc)
z.to_numpy()

Which gives the following output:

array([['45.34301499763467, -73.80926407774574'],
       ['45.8563638, -73.9631463'],
                ...
      ['45.43701969433377, -73.24177014564303']], dtype=object)

I’ve attempted to use

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

x,y = z.T

But run into a "ValueError: not enough values to unpack (expected 2, got 1)", I believe that is due to the ” inside the brackets. How do I transform this array to be able to plot it?

>Solution :

DataFrames can generally not contain more complex types, such as lists or tuples. Hence your cordinates are strings.
You can use ast.literal.eval to evaluate your strings directly from dataframe for instance with list comprehension:

import ast
z = np.array([ast.literal_eval(row) for row in df['Loc']])
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