-2

Here is the sample list given, I am using the Python 3, and want to create the same list from the given panda's DataFrame from two columns Latitude and Longitude, but I don't want to use the For Loop, want to do with the Pandasfunction.

coordinates = [
    [  42.3581    ,  -71.0636    ],
    [  42.82995815,  -74.78991444],
    [  43.17929819,  -78.56603306],
    [  43.40320216,  -82.37774519],
    [  43.49975489,  -86.20965845],
    [  43.46811941,  -90.04569087],
    [  43.30857071,  -93.86961818],
    [  43.02248456,  -97.66563267],
    [  42.61228259, -101.41886832],
    [  42.08133868, -105.11585198],
    [  41.4338549 , -108.74485069],
    [  40.67471747, -112.29609954],
    [  39.8093434 , -115.76190821],
    [  38.84352776, -119.13665678],
    [  37.7833    , -122.4167    ]]

but I want it without using the for loop, is it possible with any Panda's function? or python function.

I hope this is clear now, for those people who said its not clear question.

id101112
  • 1,012
  • 2
  • 16
  • 28
  • What's wrong with what you've posted? There are no loops there. – goodvibration Nov 01 '17 at 15:30
  • There is no `for` loop in your question. What have you tried? – roganjosh Nov 01 '17 at 15:31
  • wait I didn't make that list, I want to make it from panda's Dataframe with two columns "Lat" and "Lon" and I want the output as given there – id101112 Nov 01 '17 at 15:32
  • Possible duplicate of [Pandas DataFrame to List of Lists](https://stackoverflow.com/questions/28006793/pandas-dataframe-to-list-of-lists) – yinnonsanders Nov 01 '17 at 15:34
  • @Ravi : are you ok with dict format? – DRPK Nov 01 '17 at 15:36
  • since someone was so kind and stopped the post ...if you want to change multidim list in onedim list use this forloop. for item in coordinates: for nitem in item: print(nitem) list.append(nitem) – veritaS Nov 01 '17 at 15:50
  • @DRPK i want to pass to the some other function where it will plot all these co-ordinates on the map, so i guess dict won't work. – id101112 Nov 01 '17 at 15:53

1 Answers1

1

Based on your minimal question, I'm assuming you have a DataFrame like this:

    Lat   Lon
0    x0    y0
1    x1    y1
2    x2    y2
3    x3    y3

And you want it in a list form like this:

[[x0, y0], [x1, y1], [x2, y2], [x3, y3]]

So you just do this:

coordinates = list(zip(df.Lat.tolist(), df.Lon.tolist()))
Evan Nowak
  • 895
  • 4
  • 8