Python 将映射调用中的"pandas.core.series.Series"列表折叠到数据框中



我是python的新手,希望有人能帮我了解map。我有一个函数myfunc,它在数据帧中获取一列,并为每一列创建一个计算结果,生成一个JSON,然后将其转换为数据帧。下面是我正在做的伪代码。

例如

def myfunc (factor):

# This is the API we are posting to
str_url = "www.foourl.com"
# This is the factor we post to try and get the result
request_string = [{"foo":factor}]

header = {"content-type": "application/json","AUTH-TOKEN": "Foo"}
# We post it using our authorization and Token
response = requests.post(str_url , data=json.dumps(request_string), headers=header)

# convert response to json format and then to the dataframe
results_json = response.json()
return(pd.json_normalize(results_json))

然后,我使用下面的代码来执行我的函数,这非常有效。我可以使用result[1]访问每个结果,以获得因子[1]的数据帧结果、因子[2]的result[2]等等;类'pandas.core.sseries.series'>

# Import in the excel sheet and get the factors
df = pd.read_excel ('ref_data/search_factors.xlsx')
test = df['factor_number']
# Run the API for every factor
# Collapse the list then into a dataframe
result = test.map(myfunc)

我的问题是

由于所有结果都是数据帧,并且在结构上完全相同(5列都具有相同的名称(,有没有一种方法可以在从map 进行所有迭代后将所有内容折叠成一个数据帧

例如,我知道在R中,您可以在dplyr或类似map_df中使用bind_rows来做同样的事情。它们在python中是等价物吗?

是的,在pandas中我们有concat

df=pd.concat(result.tolist())

最新更新