1

I have a dataframe that looks like following:

Address           Unit    Zipcode
111 west street    1A      12312
132 east street    NaN     13333
90 3rd avenue      PH      22222

And I have a code to format this address using Google API:

data['input'] = data['Address'] + ', ' + data['Unit'] + ', NY, '+data['Zipcode'].map(str)
df = list(data['input'])
addList = []
formatted = ''

for index in range(len(data)):
    try:
        jsonData = requests.get('https://maps.googleapis.com/maps/api/geocode/json?address=' + df[index] + '&key=D3fsldk3FJDISDKfjeisl')
        jsonData = jsonData.content
        jsonData = json.loads(jsonData)
        data.loc[index,'formatted'] = jsonData['results'][0]['formatted']

    except:
        ("No Address")

And When I run this I get:

Address           Unit    Zipcode   input
111 west street    1A      12312     111 west street, 1A 12312
132 east street    NaN     13333     NaN
90 3rd avenue      PH      22222     90 3rd avenue, PH 22222

However, I don't get the second "123 east street 13333" because the Unit is a NULL value. My expected output would be:

Address           Unit    Zipcode   input
111 west street    1A      12312     111 west street, 1A 12312
132 east street    NaN     13333     132 east street, 13333
90 3rd avenue      PH      22222     90 3rd avenue, PH 22222

I have tried to insert for loop, however, i have an error.

for idx, row in data.iterrows():
    if data['Unit'].isna() == True:
        data['input'] = data['Address'] + ', NY, '+ data['Zipcode'].map(str)
    else:
        data['input'] = data['Address'] + ', ' + data['Unit'] + ', NY, '+data['Zipcode'].map(str)

data['formatted'] = ''
df = list(data['input'])
addList = []

for index in range(len(data)):
    try:
        jsonData = requests.get('https://maps.googleapis.com/maps/api/geocode/json?address=' + df[index] + '&key=D3fsldk3FJDISDKfjeisl')
        jsonData = jsonData.content
        jsonData = json.loads(jsonData)
        data.loc[index,'formatted'] = jsonData['results'][0]['formatted']

    except:
        ("No Address")

The error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Any help will be appreciated..!

2
  • 1
    Can't you just use fillna() to change the NA to '' Commented Mar 10, 2020 at 19:50
  • But I get empty "East 7th Street, , NY, 10009" , , in between the address.... I need to format it a certain way to match with other data. Commented Mar 10, 2020 at 19:53

1 Answer 1

1

you can use np.where such as:

data['input'] = np.where(data['Unit'].isna(), 
                         data['Address'] + ', NY, '+ data['Zipcode'].map(str), 
                         data['Address'] + ', ' + data['Unit'] + ', NY, '+data['Zipcode'].map(str))
Sign up to request clarification or add additional context in comments.

2 Comments

It still gives me the same output, such as "East 7th Street, , NY, 10009", that , , in between....
did you fillna before? because no need here

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.