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.

smci
  • 32,567
  • 20
  • 113
  • 146
  • 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]` – smci Aug 04 '19 at 13:29
  • Possible duplicate of [Why does append() return None in Python?](https://stackoverflow.com/questions/16641119/why-does-append-return-none-in-python) – smci Aug 04 '19 at 13:44

1 Answers1

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)

smci
  • 32,567
  • 20
  • 113
  • 146