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)

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

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

Leave a Reply