简体   繁体   中英

Returning multiple values in python and appending them to unique columns to a dataframe

Background:

I have a function that gets a bunch of attributes from a database. Here is the function:

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

To make a 'Gender' column I wrap the function into a lambda as so

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

Objective: I want to do this process simultaneously for all the other attributes you see below Gender, how can I do this in Python.

You can return a dictionary, then expand a series of dictionary objects:

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 {}

Then expand your series of dictionaries:

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()))

As per @spaniard's comment, if you want all available fields, you can simply use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM