21

I have two flat lists of geographical coordinates (lat, long), and I need to combine them into a 2D array or matrix.

They are now stored in a dataframe:

    lat         lon
0   48.010258   -6.156909
1   48.021648   -6.105887
2   48.033028   -6.054801
3   48.044384   -6.003691
4   48.055706   -5.952602
5   48.067017   -5.901447
6   48.078304   -5.850270
7   48.089558   -5.799114
8   48.100800   -5.747891

How can I combine these two lists into a 2D array so that the lat-lon correspondence is preserved? These are the plain data:

lat=[48.01,48.02,48.03,48.04,48.05,48.06,48.07,48.08,48.10]
lon=[-6.15,-6.10,-6.05,-6.00,-5.95,-5.90,-5.85,-5.79,-5.74]

EDIT

These excerpted data represent a (lat, long) or (y, x) geographical map. Combined, they reproduce the below image. You clearly see the presence of The intended outcome will have to be deprived of an outer frame of data of a certain width. So it's like cutting out an outer frame of a picture, the width of which is 30 data points.

FaCoffee
  • 7,609
  • 28
  • 99
  • 174
  • 2
    What result do you want? It could be done with a simple loop or with the [**`zip`**](https://docs.python.org/2/library/functions.html#zip) function. What difficulty are you having? – Peter Wood Jan 04 '17 at 16:02

5 Answers5

42
list(zip(lat, long))

gives

[(48.01, -6.15), (48.02, -6.1), (48.03, -6.05), (48.04, -6.0), 
 (48.05, -5.95), (48.06, -5.9), (48.07, -5.85), (48.08, -5.79), (48.1, -5.74)]

More on zip here

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • Please check my edit. I failed to say something crucial. – FaCoffee Jan 04 '17 at 16:04
  • @CF84 Could you be a little more specific? The more we know about the bounds of your problem the more relevant our answers will be. – Patrick Haugh Jan 04 '17 at 16:06
  • @CF84 as long as you have a dataframe `df` with a lat column and a lon column, you can do `list(zip(df.lat, df.lon))`. I don't see how removing some rows from the dataframe will effect this. – Patrick Haugh Jan 04 '17 at 16:12
9

Try using the numpy module i.e: np.column_stack maybe experiment with it to see if it gives you the desired result/format

>>> np.column_stack((lat, lon))

check out numpy.column_stack hope this helps :)

Just Ice
  • 179
  • 1
  • 1
  • 11
4

You can just explicitly add them to a new list and assign it like...

coordinates = [lat, lon]

It would then set coordinates equal to...

[
 [48.01,48.02,48.03,48.04,48.05,48.06,48.07,48.08,48.10],
 [-6.15,-6.10,-6.05,-6.00,-5.95,-5.90,-5.85,-5.79,-5.74]
]
m_callens
  • 6,100
  • 8
  • 32
  • 54
4

Adding to Patrick Haugh's answer:

np.array(list(zip(lat, lon)))

Will give:

array([[48.01, -6.15],
       [48.02, -6.1 ],
       [48.03, -6.05],
       [48.04, -6.  ],
       [48.05, -5.95],
       [48.06, -5.9 ],
       [48.07, -5.85],
       [48.08, -5.79],
       [48.1 , -5.74]])
user41855
  • 917
  • 8
  • 15
2
lat=[48.01,48.02,48.03,48.04,48.05,48.06,48.07,48.08,48.10]
lon=[-6.15,-6.10,-6.05,-6.00,-5.95,-5.90,-5.85,-5.79,-5.74]
mat = [[0]*2 for i in range(len(lat))]
k=0
for i, j in zip(lat, lon):
    mat[k][0]=i
    mat[k][1]=j
    k+=1
print (mat) 
jophab
  • 5,356
  • 14
  • 41
  • 60