spitfiredd
spitfiredd

Reputation: 3125

Apply a function of a column in dataframe that uses the values of each row as input

I am trying to apply a function that returns as input a list of the businesstypes from api.gov. To build the url it uses the duns number from a pandas dataframe.

import pandas as pd
from requests.compat import urljoin, quote_plus

def get_business_types(c):
    base_url = 'https://api.data.gov/sam/v1/registrations/'
    duns = c['duns_normal']
    final_url = base_url + duns + '0000'
    request = requests.get(final_url, params=params)
    data = request.json()
    result  = data['sam_data']['registration']['businessTypes']
    return result

duns['business_types'] =duns['duns_normal'].apply(get_business_types)

I am kind of unclear on the line duns = c['duns_normal'] is this correct? How do I get the duns number so I can build the url?

Upvotes: 1

Views: 80

Answers (1)

Nil
Nil

Reputation: 42

when you are using apply on a column, it passes one value at a time. So the below would suffice.

import pandas as pd
from requests.compat import urljoin, quote_plus

def get_business_types(c):
    print(c)
    base_url = 'https://api.data.gov/sam/v1/registrations/'
    #duns = c['duns_normal']
    final_url = base_url + str(c) + '0000'
    request = requests.get(final_url, params=params)
    data = request.json()
    result  = data['sam_data']['registration']['businessTypes']
    return result

duns['business_types'] =duns['duns_normal'].apply(get_business_types)

Upvotes: 1

Related Questions