SmartyStreets API 在数据框中循环
SmartyStreets API looped across a data frame
我是 Python 的新手,正在使用 SmartyStreets API。我正在尝试读取包含地址的数据框并简单地验证它们。但是,我正在 运行 解决问题,但没有产生任何结果。下面的代码应该只是在末尾为数据框中的每一行输出打印语句。
我希望函数从包含正确信息的数据框中的列中获取其变量的名称。
例如:
读入数据时,该函数应从 Street1 列获取其地址变量。同样,City,应该来自 City 列,State 来自 state,Zipcode 来自 zipcode。
因此对于第一次迭代,函数应按如下方式存储变量:
lookup.street = '1600 Amphitheatre Pkwy'
lookup.city = 'Mountain view"
lookup.state = 'CA'
lookup.zip = '94043'
最终目标是让它在每一行的末尾附加一个县列,并使用 API 中的适当县。但是,我无法在没有错误的情况下将其发送到 运行。任何帮助都会有所帮助。如果需要,您可以从 SmartyStreets 网站获得每月 250 次通话的免费 API 密钥。我包含的代码来自 github 上的示例。这可能不是最有效的方法,如果您有更有效的编码方法,请随时将其包含在您的回复中。使用的代码和发现的错误如下所示。
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'))
当这个 运行s 时,我得到以下错误:
TypeError: ("'NoneType' object is not callable", 'occurred at index
Steet1')
据我所知,存在一些问题。首先,你的字典调用第一列 '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,但那说它仍然应该工作。
我是 Python 的新手,正在使用 SmartyStreets API。我正在尝试读取包含地址的数据框并简单地验证它们。但是,我正在 运行 解决问题,但没有产生任何结果。下面的代码应该只是在末尾为数据框中的每一行输出打印语句。
我希望函数从包含正确信息的数据框中的列中获取其变量的名称。
例如: 读入数据时,该函数应从 Street1 列获取其地址变量。同样,City,应该来自 City 列,State 来自 state,Zipcode 来自 zipcode。
因此对于第一次迭代,函数应按如下方式存储变量:
lookup.street = '1600 Amphitheatre Pkwy'
lookup.city = 'Mountain view"
lookup.state = 'CA'
lookup.zip = '94043'
最终目标是让它在每一行的末尾附加一个县列,并使用 API 中的适当县。但是,我无法在没有错误的情况下将其发送到 运行。任何帮助都会有所帮助。如果需要,您可以从 SmartyStreets 网站获得每月 250 次通话的免费 API 密钥。我包含的代码来自 github 上的示例。这可能不是最有效的方法,如果您有更有效的编码方法,请随时将其包含在您的回复中。使用的代码和发现的错误如下所示。
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'))
当这个 运行s 时,我得到以下错误:
TypeError: ("'NoneType' object is not callable", 'occurred at index Steet1')
据我所知,存在一些问题。首先,你的字典调用第一列 '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,但那说它仍然应该工作。