首页 > 解决方案 > 循环将数据帧与 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: []

不确定这是否是将数据框合并在一起的正确方法?

标签: pandasdataframe

解决方案


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()

推荐阅读