pandas - 循环将数据帧与 groupby sum 合并在一起
问题描述
我有一个函数通过 API 循环以获取结果并将其解析为数据帧:
consolidated_branch = DataFrame(columns=['Inventory', 'Item1', 'Item2', 'Results'])
for each_branch in branches:
branch_result = get_branches_details(each_branch) # get_branch_details returns a dataframe
print(branch_result)
# Loop results:
Inventory ... Results
Year ...
2015 51746398 ... 1090532
2016 33864077 ... -6915080
2017 43410104 ... -3224172
2018 48753351 ... -679117
[4 rows x 9 columns]
Inventory ... Results
Year ...
2017 2028092 ... 6100009
2018 2150537 ... 17570443
[2 rows x 9 columns]
......
如何将每个分支汇总到consolidated_branch
数据框中?
我试过了:
for each_branch in get_branch_details(branches):
consolidated_branch = concat([consolidated_branch, branch_result], axis=0, ignore_index=True).groupby(["Item1"]).sum()
print(consolidated_branch)
# results
Empty DataFrame
Columns: [Inventory, Item1, Item2, Results]
Index: []
不确定这是否是将数据框合并在一起的正确方法?
解决方案
DataFrame
首先在列表理解中创建s 列表:
L = [get_branches_details(each_branch) for each_branch in branches]
或循环:
L = []
for each_branch in branches:
L.append(get_branches_details(each_branch))
然后concat
与聚合一起使用sum
:
branch_result = pd.concat(L, axis=0, ignore_index=True).groupby(["Item1"]).sum()
推荐阅读
- python - 我如何知道我的文件是使用 PyPDF2 附加在我的 PDF 中的?
- typescript - 打字稿泛型 - 类型“X”不能用于索引类型“Y”
- java - 避免observeOn的副作用
- sql - “FETCH FIRST”之前/之后“OFFSET”的区别
- javascript - 为什么我的 javascript 联系表单不起作用?
- java - Android的android studio主要活动中对话框的放置
- css - 材质 UI 列表、ListItemText 中的 NO WRAP 挑战
- python - MongoDB(pymongo)查找并推送到嵌套数组
- vue.js - 如何在插件中访问 Vue $refs?
- xml - 在 Scala 中更改函数的返回类型