在数据框中应用列的函数,该函数使用每行的值作为输入

时间:2017-02-25 13:27:52

标签: python pandas

我正在尝试应用一个函数,该函数返回api.gov中businesstypes列表作为输入。要构建url,它使用pandas数据帧中的duns编号。

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)

我有点不清楚duns = c['duns_normal']这是正确的吗?如何获得duns号码以便我可以建立网址?

1 个答案:

答案 0 :(得分:1)

在列上使用apply时,它一次传递一个值。所以下面就足够了。

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)