对于此函数:
def getData(key, full_name, address):
url = 'https://personator.melissadata.net/v3/WEB/ContactVerify/doContactVerify?id=' + key +'&full='
+ full_name +'&a1=' + address
xml = request.get(url)
dom = parseString(xml.text)
response = dom.getElementsByTagName('response')
Gender = getElementValue(dom, 'Gender')
DateOfBirth = getElementValue(dom, 'DateOfBirth')
DateOfDeath = getElementValue(dom, 'DateOfDeath')
EthnicCode = getElementValue(dom, 'EthnicCode')
EthnicGroup = getElementValue(dom, 'EthnicGroup')
Education = getElementValue(dom, 'Education')
PoliticalParty = getElementValue(dom, 'PoliticalParty')
MaritalStatus = getElementValue(dom, 'MaritalStatus')
HouseholdSize = getElementValue(dom, 'HouseholdSize')
ChildrenAgeRange = getElementValue(dom, 'ChildrenAgeRange')
PresenceOfChildren = getElementValue(dom, 'PresenceOfChildren')
PresenceOfSenior = getElementValue(dom, 'PresenceOfSenior')
LengthOfResidence = getElementValue(dom, 'LengthOfResidence')
OwnRent = getElementValue(dom, 'OwnRent')
CreditCardUser = getElementValue(dom, 'CreditCardUser')
Occupation = getElementValue(dom, 'Occupation')
HouseholdIncome = getElementValue(dom, 'HouseholdIncome')
return Gender
当我将其包装到lambda中时:
df['Gender'] = df.apply(lambda row: getData(key, row['Full Name'], row['Address']), axis = 1)
我收到此错误:
TypeError: ("bad operand type for unary +: 'str'", 'occurred at index 0')
这里发生:
2 url = 'https://personator.melissadata.net/v3/WEB/ContactVerify/doContactVerify?id=' + key +'&full='
----> 3 + full_name +'&a1=' + address
如何解决此问题?
为了完整起见,这里是整个代码(没有Melissa键):
from xml.dom.minidom import parse,parseString
import xml.dom.minidom
import requests
import sys
import pandas as pd
import numpy as np
key = ''
df = pd.read_csv('data.csv')
def getElementValue(p_dom,p_element):
if len(p_dom.getElementsByTagName(p_element)) > 0:
l_value=p_dom.getElementsByTagName(p_element)[0]
return(l_value.firstChild.data)
else:
l_value='NaN'
return(l_value)
def getData(key, full_name, address):
url = 'https://personator.melissadata.net/v3/WEB/ContactVerify/doContactVerify?id=' + key +'&full='
+ full_name +'&a1=' + address
xml = request.get(url)
dom = parseString(xml.text)
response = dom.getElementsByTagName('response')
Gender = getElementValue(dom, 'Gender')
DateOfBirth = getElementValue(dom, 'DateOfBirth')
DateOfDeath = getElementValue(dom, 'DateOfDeath')
EthnicCode = getElementValue(dom, 'EthnicCode')
EthnicGroup = getElementValue(dom, 'EthnicGroup')
Education = getElementValue(dom, 'Education')
PoliticalParty = getElementValue(dom, 'PoliticalParty')
MaritalStatus = getElementValue(dom, 'MaritalStatus')
HouseholdSize = getElementValue(dom, 'HouseholdSize')
ChildrenAgeRange = getElementValue(dom, 'ChildrenAgeRange')
PresenceOfChildren = getElementValue(dom, 'PresenceOfChildren')
PresenceOfSenior = getElementValue(dom, 'PresenceOfSenior')
LengthOfResidence = getElementValue(dom, 'LengthOfResidence')
OwnRent = getElementValue(dom, 'OwnRent')
CreditCardUser = getElementValue(dom, 'CreditCardUser')
Occupation = getElementValue(dom, 'Occupation')
HouseholdIncome = getElementValue(dom, 'HouseholdIncome')
return Gender
df['Gender'] = df.apply(lambda row: getData(key, row['Full Name'], row['Address']), axis = 1)
我已经使用相同的方法成功地从 zillow 和 mapquest 中提取数据,但我似乎无法让它为 Melissa 工作。
你试过这个吗?
url = 'https://personator.melissadata.net/v3/WEB/ContactVerify/doContactVerify?id=' + str(key) +'&full='
+ str(full_name) +'&a1=' + str(address)
问题是这一行:
+ full_name +'&a1=' + address
似乎您希望它是上一行的延续,但事实并非如此。