Home > other >  How to store results of an API call that creates records with python into pandas df
How to store results of an API call that creates records with python into pandas df

Time:10-03

I am trying to create a new customer in my billing systems and then receive a response back because I need the newly created customer id in pandas df

This is what I have so far - when the results are returned - they will return the same JSON there will be a customer id and created time stamp - and I need to store all those results into a pandas df to work with further. This is what I have below - from my understanding - I need to append the df - but all examples I've seen so far work with 1 value - how do I do it with multiple values?

new_customers = pd.DataFrame()
for customer,r in sh2_ws_df.iterrows():
    cb.Customer.create({
        "first_name": r['Billing First Name'],
        "last_name": r['Billing Last Name'],
        "email": r['Billing Email'],
        "phone": r['Billing Phone'],
        "company": r['Account Name'],
        "auto_collection": "on",
        "net_term_days": 0,
        "allow_direct_debit": 'true',
        "taxability": "taxable",
        "locale": "en",
        "cf_referral_partner": r['cb_referral_partner'],
        "cf_business_type": r['cf_business_type'],
        "billing_address" : {
            "first_name" : r['Billing First Name'],
            "last_name" : r['Billing Last Name'],
            "email": r['Billing Email'],
            "company": r['Account Name'],
            "phone": r['Billing Phone'],
            "line1" : r['Billing Address 1'],
            "line2": r['Billing Address 2'],
            "city" : r['Billing City'],
            "state" : r['Billing State'],
            "zip" : r['Billing Zip'],
            "country" : r['Billing Country']
        }
}
    new_customers.append()
    )

CodePudding user response:

According to pandas documentation, append is deprecated so better use concat.

new_customers = pd.DataFrame()
for customer,r in sh2_ws_df.iterrows():
    # I assume that a DataFrame cust is returned
    cust = cb.Customer.create({
        "first_name": r['Billing First Name'],
        "last_name": r['Billing Last Name'],
        "email": r['Billing Email'],
        "phone": r['Billing Phone'],
        "company": r['Account Name'],
        "auto_collection": "on",
        "net_term_days": 0,
        "allow_direct_debit": 'true',
        "taxability": "taxable",
        "locale": "en",
        "cf_referral_partner": r['cb_referral_partner'],
        "cf_business_type": r['cf_business_type'],
        "billing_address" : {
            "first_name" : r['Billing First Name'],
            "last_name" : r['Billing Last Name'],
            "email": r['Billing Email'],
            "company": r['Account Name'],
            "phone": r['Billing Phone'],
            "line1" : r['Billing Address 1'],
            "line2": r['Billing Address 2'],
            "city" : r['Billing City'],
            "state" : r['Billing State'],
            "zip" : r['Billing Zip'],
            "country" : r['Billing Country']
        }
    }
    # at the end of the for loop, concatenate the new customer 'cust' with the new_customers DataFrame
    new_customers = pd.concat([new_customers, cust], ignore_index=True)

Note: Couldn't see which variable was storing the data for the new customer so I added the variable cust. Adapt yourself accordingly.

  • Related