python - 在 python 中返回多个值并将它们附加到数据帧的唯一列

标签 python pandas lambda

背景:

我有一个函数可以从数据库中获取一堆属性。这是函数:

def getData(key, full_name, address, city, state, zipcode):
    try:
        url = 'https://personator.melissadata.net/v3/WEB/ContactVerify/doContactVerify'
        payload={
                'TransmissionReference': "test", # used by you to keep track of reference
                'Actions': 'Check',
                'Columns': 'Gender','DateOfBirth','DateOfDeath','EthnicCode','EthnicGroup','Education','PoliticalParty','MaritalStatus','HouseholdSize','ChildrenAgeRange','PresenceOfChildren','PresenceOfSenior','LengthOfResidence','OwnRent','CreditCardUser','Occupation','HouseholdIncome',
                'CustomerID': key,# key
                'Records': [{'FullName': str(full_name), 'AddressLine1': str(address), 'City': str(city), 'State': str(state), 'PostalCode': str(zipcode)}]
                }
        headers = {'Content-Type': 'application/json; charset=utf-8', 'Accept':'application/json', 'Host':'personator.melissadata.net','Expect': '100-continue', 'Connection':'Keep-Alive'}
        r = requests.post(url, data=json.dumps(payload), headers=headers)
        dom = json.loads(r.text)

        Gender = dom['Records'][0]['Gender']
        DateOfBirth = dom['Records'][0]['DateOfBirth']
        DateOfDeath = dom['Records'][0]['DateOfDeath']
        EthnicCode = dom['Records'][0]['EthnicCode']
        EthnicGroup = dom['Records'][0]['EthnicGroup']
        Education = dom['Records'][0]['Education']
        PoliticalParty = dom['Records'][0]['PoliticalParty']
        MaritalStatus = dom['Records'][0]['MaritalStatus']
        HouseholdSize = dom['Records'][0]['HouseholdSize']
        ChildrenAgeRange = dom['Records'][0]['ChildrenAgeRange']
        PresenceOfChildren = dom['Records'][0]['PresenceOfChildren']
        PresenceOfSenior = dom['Records'][0]['PresenceOfSenior']
        LengthOfResidence = dom['Records'][0]['LengthOfResidence']
        OwnRent = dom['Records'][0]['OwnRent']
        CreditCardUser = dom['Records'][0]['CreditCardUser']
        Occupation = dom['Records'][0]['Occupation']
        HouseholdIncome = dom['Records'][0]['HouseholdIncome']

        return Gender
    except:
        return None

为了创建“性别”列,我将函数包装到 lambda 中,如下所示

df['Gender'] = df.apply(lambda row: getData(key, row['Full Name'], row['Address'], row['City'], row['State'], row['Zipcode']))

目标: 我想对您在“性别”下面看到的所有其他属性同时执行此过程,如何在 Python 中执行此操作。

最佳答案

您可以返回一个字典,然后展开一系列字典对象:

fields = ['Gender', 'DateOfBirth', etc.]

def getData(key, full_name, address, city, state, zipcode):
    try:
        # your code as before
        dom = json.loads(r.text)
        return {k: dom['Records'][0][k] for k in fields}
    # modify below: good practice to specify exactly which error(s) to catch
    except:
        return {}

然后扩展您的词典系列:

dcts = df.apply(lambda row: getData(key, row['Full Name'], row['Address'], row['City'],
                                    row['State'], row['Zipcode']), axis=1)

df = df.join(pd.DataFrame(dcts.tolist()))

根据 @spaniard 的评论,如果您想要所有可用字段,您可以简单地使用:

return json.loads(r.text)['Records'][0]

关于python - 在 python 中返回多个值并将它们附加到数据帧的唯一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54337665/

相关文章:

java - 方法 "reference to an instance method of an arbitrary object of a particular type"如何解析任意对象?

python - Matplotlib 无对象 'Use' 或 '__version__'

python-2.7 - Pandas:以特定格式生成日期范围

c#:Enumerable 类中的 Where()、OrderBy() 和 Select() 不应该采用委托(delegate)类型、lambda 表达式或匿名类型作为参数

python - 循环遍历 pandas Dataframe 以从另一个 Dataframe 获取值

python - 与 Pandas 并排绘制的箱线图

c# - 按递增顺序将列表拆分为多个列表

python - 如何为 virtualenvwrapper-win 设置环境变量 WORKON_HOME

python - AWS Cloudwatch Logstream - 关键是什么,以及在获取日志流时如何设置它

python - 如何定义修改后的leaky ReLU - TensorFlow