3

I am fairly new to Python, and am working with the SmartyStreets API. I am trying to read in a data frame that contains addresses and simply validate them. However, I am running into issues and not producing any results. The below code should simply output the print statements at the end for each of the rows in the data frame.

I would like the function to get the names of its variables from the columns in the data frame that contains the proper information.

For example: When reading in the data, the function should get its address variable from the Street1 column. Likewise, City, should come from City column, State from state and Zipcode from zipcode.

So for the first iteration, the function should store the variables as follows:

lookup.street = '1600 Amphitheatre Pkwy'
lookup.city = 'Mountain view"
lookup.state = 'CA'
lookup.zip = '94043'

The ultimate goal will be to get this to append a county column to the end of each row with the proper county from the API. However, I am not able to get this to run without errors. Any assistance would be helpful. If needed, you can obtain a free API key for 250 calls per month from the SmartyStreets website. The code that I have included comes from the examples on github. This may not be the most efficient way to do this, if you have a more efficient way to code this, feel free to include that in your response. The code used and error found is shown below.

import pandas as pd
from smartystreets_python_sdk import StaticCredentials, exceptions, ClientBuilder
from smartystreets_python_sdk.us_street import Lookup as StreetLookup

##Defines Dictionary
dict = {'Street1': ["1600 Amphitheatre Pkwy", "1 Infinite Loop", "1 Rosedale"],
        'City': ['Mountain view', 'Cupertino', 'Baltimore'],
        'State': ['CA', 'CA', 'MD'],
        'Zipcode': ['94043', '95014', '21237']}
##Converts Dictionary to Data Frame
df = pd.DataFrame(dict)

##Defines Function
def run(address = '', city = '', state = '', zipcode = ''):
    
    auth_id = 'ID HERE'
    auth_token = 'TOKEN HERE'
    
    credentials = StaticCredentials(auth_id, auth_token)
    
    client = ClientBuilder(credentials).build_us_street_api_client()
    
        
    lookup = StreetLookup()
    #lookup.input_id = '' ##Optional ID from your system
    #lookup.addressee = addressee
    lookup.street = address
    ##lookup.street2 = address2
    #lookup.secondary = secondary ##STE, Apartment, etc.
    #lookup.urbanization = '' ##PR Addresses ONLY
    lookup.city = city
    lookup.state = state
    lookup.zipcode = zipcode
    lookup.candidates = 1
    lookup.match = 'Invalid'

    try:
        client.send_lookup(lookup)
    except exceptions.SmartyException as err:
        print(err)
        return

    result = lookup.result

    if not result:
        print("No candidates. This means the address is not valid.")
        return

    first_candidate = result[0]

    print("Address is valid. (There is at least one candidate)\n")
    print("ZIP Code: " + first_candidate.components.zipcode)
    print("County: " + first_candidate.metadata.county_name)

    for c, candidate in enumerate(lookup.result):
        print("- {}: {}, {}, {}".format(c, candidate.delivery_line_1, candidate.last_line, candidate.metadata.county_name))

##Runs function     
df.apply(run(address = 'Street1',  city = 'City', state = 'State', zipcode = 'Zipcode'))

When this runs, I get the following error:

TypeError: ("'NoneType' object is not callable", 'occurred at index Steet1')

4

1 回答 1

1

据我所知,有几个问题。首先,您的字典称第一列为“Steet1”,但应该是“Street1”。其次,我认为这不是 apply 的正确用法。如果您想做这样的事情,我建议进行以下更改。

def run(row):
    address = row['Street1']
    city = row['City']
    state = row['State']
    zipcode = row['Zipcode']
    ...
df.apply(run, axis = 1)

试试看,如果能解决问题,请告诉我。此外,由于您只是使用函数打印内容,因此run您可以使用 for 循环循环并调用该函数,因此不一定需要使用 apply,但这表示它仍然应该工作。

于 2020-01-08T18:43:30.040 回答