Merging two columns from Google Sheets into this confusing data structure

Viewed 65

I'm pulling two columns from Google Sheets. They contain lat/long information. I can print them just fine with this code:

x1 = [item1 for item1 in wks.col_values(3) if item1]
for item1 in x1[1:]:
    print(item1)
    print(Decimal(item1))

x2 = [item2 for item2 in wks.col_values(4) if item2]
for item2 in x2[1:]:
    print(item2)
    print(Decimal(item2))

output:

39.204345
39.204345
39.204345
39.204345
39.602555
39.602555
39.802999
39.802999
-76.67478
-76.67478
-76.674806
-76.674806
-76.674822
-76.674822
-76.374822
-76.374822

How can I get all of these values and any future values to work with this type of data structure?:

latitude, longitude = zip(*[
    (37.769901, -122.498331),
    (37.768645, -122.475328),
    (37.771478, -122.468677),
    (37.769867, -122.466102),
    (37.767187, -122.467496),
    (37.770104, -122.470436)
])

In other words, is there a way to pull both columns lat and long, and zip them like above? This is for use with gmplot.

edit: additional errors. nothing but errors. tried this so many different ways....

latitude = int(float(wks.col_values(3)))
longitude = int(float(wks.col_values(4)))
##latitude, longitude = np.ndarray(*zip(x1[1:], x2[1:]))

apikey = '' # (your API key here)
gmap = gmplot.GoogleMapPlotter(37.766956, -122.448481, 14, apikey=apikey)
gmap.scatter(latitude, longitude, color='#FF0000', size=75, marker=False)
gmap.draw('newmap.html')



TypeError: float() argument must be a string or a number, not 'list'
1 Answers

Seeing that x1 and x2 have the same length, you could say:

latitude, longitude = zip(*[(x1[i], x2[i]) for i in range(1,len(x1) - 1)])

Alternatively, you can use slice notation and not worry about lengths:

latitude, longitude = zip(*zip(x1[1:], x2[1:]))
Related