python - 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] 的结果 [2] 等等。它返回一个 <class 'pandas.core.series.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列都具有相同的名称),有没有一种方法可以在地图的所有迭代之后将所有内容折叠成一个数据框
例如,我知道R
你可以使用 bind_rowsdplyr
或类似的东西map_df
来做同样的事情。它们在python中是等价的吗?
解决方案
是的,pandas
我们有concat
df=pd.concat(result.tolist())
推荐阅读
- ruby-on-rails - 如何重定向到呈现反应rails页面的ruby on rails控制器
- javascript - 如何在对象内部使用解构赋值
- bash - 在linux中使用file命令向文件添加扩展名
- ruby-on-rails - Rails 5:视图/控制器中未初始化的常量,但不是控制台
- r - 在 mlr 和 parallelMap 中可以并行化多个级别吗?例如 mlr.tuneParams 和 mlr.benchmark
- jboss - log4j2 似乎没有遵循我的日志滚动策略
- reactjs - 将 fetch 操作转换为 axios 操作
- c# - “必须声明标量变量@dni”
- c# - 如何修复清单中的不匹配引用和下载的程序集 LINQPad.exe 的标识
- javascript - 如何将相同的选项附加到多个选择下拉框?