python - Pandas Google Distance Matrix API - Pass coordinates into URL -


i working google distance matrix api, want feed coordinates dataframe api , return duration , distance between 2 points. here dataframe:

import pandas pd import simplejson import urllib import numpy np  record   orig_lat    orig_lng       dest_lat     dest_lng       1        40.7484405  -74.0073127    40.7115242   -74.0145492    2        40.7421218  -73.9878531    40.7727216   -73.9863531    

first, need combine orig_lat & orig_lng , dest_lat & dest_lng strings, pass url. i've tried creating variables orig_coord & dest_coord passing them url , returning values:

orig_coord = df[['orig_lat','orig_lng']].apply(lambda x: '{},{}'.format(x[0],x[1]), axis=1) dest_coord = df[['dest_lat','dest_lng']].apply(lambda x: '{},{}'.format(x[0],x[1]), axis=1)  row in df.itertuples():     url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins={0}&destinations={1}&units=imperial&mygoogleapikey".format(orig_coord,end_coord)     result = simplejson.load(urllib.urlopen(url))     df['driving_time_text'] = result['rows'][0]['elements'][0]['duration']['text'] 

but following error: "typeerror: () got unexpected keyword argument 'axis'"

so question is: how concatenate values 2 columns string, pass string url , output result?

thank in advance!

hmm, not sure how constructed data frame. maybe post details? if can live referencing tuple elements positionally, worked me:

import pandas pd  data = [{'orig_lat': 40.748441, 'orig_lng': -74.007313, 'dest_lat': 40.711524, 'dest_lng': -74.014549},          {'orig_lat': 40.742122, 'orig_lng': -73.987853, 'dest_lat': 40.772722, 'dest_lng': -73.986353}] df = pd.dataframe(data) row in df.itertuples():  orig_coord='{},{}'.format(row[1],row[2])  dest_coord='{},{}'.format(row[3],row[4])  url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins={0}&destinations={1}&units=imperial&mygoogleapikey".format(orig_coord,dest_coord)  print url 

produces

http://maps.googleapis.com/maps/api/distancematrix/json?origins=40.748441,-74.007313&destinations=40.711524,-74.014549&units=imperial&mygoogleapikey http://maps.googleapis.com/maps/api/distancematrix/json?origins=40.742122,-73.987853&destinations=40.772722,-73.986353&units=imperial&mygoogleapikey 

to update data frame result, since row tuple , not writeable, might want keep track of current index iterate. maybe this:

data = [{'orig_lat': 40.748441, 'orig_lng': -74.007313, 'dest_lat': 40.711524, 'dest_lng': -74.014549, 'result': -1},          {'orig_lat': 40.742122, 'orig_lng': -73.987853, 'dest_lat': 40.772722, 'dest_lng': -73.986353, 'result': -1}] df = pd.dataframe(data) i_row = 0 row in df.itertuples():  orig_coord='{},{}'.format(row[1],row[2])  dest_coord='{},{}'.format(row[3],row[4])  url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins={0}&destinations={1}&units=imperial&mygoogleapikey".format(orig_coord,dest_coord)  # stuff result  df['result'][i_row] = result  i_row++ 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -