我是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]等。它返回
# 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
中使用bind_rows或类似map_df
之类的东西来做同样的事情。它们在python中是否等效?
答案 0 :(得分:1)
是的,在pandas
中,我们有concat
df=pd.concat(result.tolist())