John Doe
John Doe

Reputation: 17

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

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?

Upvotes: 0

Views: 21

Answers (1)

Anynamer
Anynamer

Reputation: 334

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

Upvotes: 1

Related Questions