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

如何将DataFrame中存储接口返回结果的Series类型列拆分后合并到原表

实现方法

方法1:手动提取指定字段(适合仅需少量字段的场景)

直接对嵌套的assignment_group_response列使用apply方法取出目标字段即可,示例代码如下:

# 示例提取attested_date和skip_sync两个字段
df['attested_date'] = df['assignment_group_response'].apply(lambda x: x['result']['attested_date'])
df['skip_sync'] = df['assignment_group_response'].apply(lambda x: x['result']['skip_sync'])

如果存在部分接口返回无result字段或目标字段缺失的情况,可以加判空逻辑避免报错:

df['attested_date'] = df['assignment_group_response'].apply(lambda x: x.get('result', {}).get('attested_date', ''))

方法2:批量展开嵌套结构(适合需要提取多个字段的场景)

如果需要提取的字段数量较多,可以用pandas自带的json_normalize方法直接把嵌套字典拍平为结构化的DataFrame,再和原表拼接,不用逐个手写提取逻辑:

# 展开嵌套的返回结果
expanded_res = pd.json_normalize(df['assignment_group_response'].apply(lambda x: x['result']))
# 可选:给展开后的列加前缀,避免和原表字段重名
expanded_res = expanded_res.add_prefix('assign_group_')
# 按索引对齐拼接回原表
df = pd.concat([df.reset_index(drop=True), expanded_res.reset_index(drop=True)], axis=1)

可选性能优化

你当前的逐行请求逻辑是串行执行,数据量较大时耗时会比较长,可以用多线程并发请求提速:

from concurrent.futures import ThreadPoolExecutor

def get_data_from_link(data):
    return requests.get(data['link'],auth=(usr,psw),headers=headers).json()

# max_workers可以根据接口的限流规则调整,一般设置为5~20即可
with ThreadPoolExecutor(max_workers=10) as executor:
    df['assignment_group_response'] = list(executor.map(get_data_from_link, df['assignment_group']))

内容的提问来源于stack exchange,提问作者Felipe Sales Mendes

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 06:36:05