0

I'm trying to add values into a new pandas column sp['New Address'] by using a Google Maps API Key and a for-loop that uses the addresses in the column sp['ASL Address (PAPER)-REFERENCE'] as a string query.

I've tried to create an empty list (addresses) that appends the searched addresses when the for-loop is run, and then run another for-loop that is supposed to loop through each row in sp['New Address'] and paste the addresses searched in the original for-loop:

for address in sp['ASL Address (PAPER)-REFERENCE']:
    api_key = "API Key"
    url = "https://maps.googleapis.com/maps/api/place/textsearch/json?"
    query = address
    r = requests.get(url + 'query=' + query + '&key=' + api_key)
    x = r.json()
    z = x['results']
    for i in range(len(z)):
        for row in sp['New Address']:
            addresses = []
            sp['New Address'] = addresses.append((z[i]['formatted_address']))

I expected the rows in sp['New Address'] to be filled with an address such as '21 Laurel Brook Rd, Middletown, CT 06457, USA'. However whenever I run the code, sp['New Address'] is still empty.

2
  • You haven't given us any reproducible data. Just shows us one value of address. In fact we probably don't need your outer loop (for address...) at all. And api_key, url can be moved outside the loop to the top. So start us at the r = requests.get(problem_url). Next, try to debug it yourself by printing the values of loop-variables like i and row. Also, you don't need to use an index for i in range(len(z)):. Just directly do for result in x['results']. No need to reference it as z[i] Commented Aug 4, 2019 at 13:29
  • Possible duplicate of Why does append() return None in Python? Commented Aug 4, 2019 at 13:44

1 Answer 1

1

Your bug is simply that .append() on a list always returns None, because it works in-place

(But if you'd attempted to debug this with a few humble print statements in each loop, you would have found that by yourself)

Sign up to request clarification or add additional context in comments.

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.