0

I'm a newbie to Python but attempting to call Google Distance Matrix API

This is how my data frame looks like

enter image description here

Loading data into data frame

data = pd.read_csv(input_filename, encoding ='utf8')

I just need some help looping the list.

Issue: It keeps on printing the entire list

#Column name in your input data
start_latitude_name = "Start Latitude"
start_longitude_name = "Start Longitude"

end_latitude_name = "End Latitude"
end_longitude_name = "End Longitude"

start_latitude_names = data[start_latitude_name].tolist()
end_latitude_names = data[end_latitude_name].tolist()

start_longitude_names = data[start_longitude_name].tolist()
end_longitude_names = data[end_longitude_name].tolist()


for start_latitude_name in start_latitude_names: 
    origins = start_latitude_name, start_longitude_name
    destinations = end_latitude_name, end_longitude_name
    mode = "walking"

    # Set up your distance matrix url
    distancematrix_url = "*Omitted unnecessary parts*origins={0}&destinations={1}&mode={2}&language=en-EN&key={3}".format(origins, destinations, mode, API_KEY)

    print(distancematrix_url)

Current Output (From each loop)

# Omitted unnecessary info
origins=40.7614645,123.0,-73.9825913,456.0&destinations=40.65815,789.0,-73.98283,0.0

Expected Output (From each loop)

origins=40.7614645,-73.9825913&destinations=40.65815,-73.98283

I'm sure that i'm not looping it correctly, but i have tried the answers on several post and it didn't work work for me. I'm open to better alternatives of looping the data. Feel free to correct me.

Thanks!

1
  • I think you need to switch the variables: startlatitude and start_latitude_names. Because otherwise your loop is unnecessary. Commented Dec 11, 2018 at 9:37

2 Answers 2

1

You could do this with pandas and df.iterrows():

import pandas as pd

data = pd.read_csv(input_filename, encoding ='utf8')

for idx, row in data.iterrows(): 
    origins = row['Start Latitude'], row['Start Longitude']
    destinations = row['End Latitude'], row['End Longitude']
    mode = "walking"

    # Set up your distance matrix url
    distancematrix_url = "*Omitted unnecessary parts*origins={0}&destinations={1}&mode={2}&language=en-EN&key={3}".format(origins, destinations, mode, API_KEY)

    print(distancematrix_url)
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thanks for ur help. I tried this before but it didn't work for me. It still prints out the entire list.
That is strange. Can you try printing row and origins before your call of distancematrix_url? Does it look correct there?
Hi, i realized that i forgot to restart my kernel. Sorry about that. Silly mistake. No wonder it didn't work. Will just go ahead and mark this as correct.
1

If I've understood correctly, you can vectorize this operation and use the string-representations of your coordinates:

import pandas as pd

# Make pandas print entire strings without truncating them
pd.set_option("display.max_colwidth", -1)

# Create dummy-df from your example
df = pd.DataFrame({"start_latitude": [40.76, 123.00], "start_longitude": [-73.98, 456.00], "end_latitude": [40.65, 789.00], "end_longitude": [-73.98, 0.00]})
print df

# Set globals
mode = "walking"
API_KEY = "my_key"

# Create the url strings for each row
df["distance_matrix_url"] = "origins=" + df["start_latitude"].map(str) + "," + df["start_longitude"].map(str) + "&destinations=" + df["end_latitude"].map(str) + "," + df["end_longitude"].map(str) + "&mode=" + mode + "&languge=en-EN&key=" + API_KEY

# Print results
print df

Output:

   end_latitude  end_longitude  start_latitude  start_longitude                                                                   distance_matrix_url
0  40.65        -73.98          40.76          -73.98            origins=40.76,-73.98&destinations=40.65,-73.98&mode=walking&languge=en-EN&key=my_key
1  789.00        0.00           123.00          456.00           origins=123.0,456.0&destinations=789.0,0.0&mode=walking&languge=en-EN&key=my_key

Is this what you're looking for?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.