You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将API返回的嵌套JSON字典列表合并为单个pandas DataFrame

解决方案

要把jsp_full存储的嵌套字典列表合并为目标格式的DataFrame,只需要先将嵌套结构展开为一维字典列表,再直接调用pandas的DataFrame方法即可,具体实现如下:

合并嵌套列表

方案1:列表推导式打平后生成

# 遍历所有分群的子列表,提取所有记录拼成一维字典列表
flat_records = [record for segment in jsp_full for record in segment]
# 直接生成目标DataFrame
full_df = pd.DataFrame(flat_records)

方案2:逐个转换后拼接

如果不想调整原有列表结构,也可以把每个分群的子列表先转成DataFrame再合并:

# 把每个分群的字典列表转成DataFrame存入列表
df_segments = [pd.DataFrame(segment) for segment in jsp_full]
# 合并所有分群DataFrame,重置索引避免重复
full_df = pd.concat(df_segments, ignore_index=True)

调整后续统计代码

你原有统计代码只取了第一个分群的jsp_full[0],调整后直接使用全量的full_df即可:

Advocacy_Supporters = full_df.sort_values("Type").groupby("Type", as_index=False)["Email"].first()
print(Advocacy_Supporters['Email'].count())

print("The number of Unique Advocacy Supporters is :")
Advocacy_Supporters_Group = full_df.groupby("Type")["Email"].nunique()
print(Advocacy_Supporters_Group)

生成的full_df完全符合你要求的输出格式,自动匹配SupporterID、Email、Type三列,每行对应一条用户记录。


内容的提问来源于stack exchange,提问作者Antonio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 13:51:04