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

如何选择性提取指定条件的嵌套字典数据并转换为Pandas DataFrame

选择性提取嵌套字典数据生成Pandas DataFrame

要实现从嵌套字典中筛选指定条目并提取目标字段生成DataFrame,可按以下步骤操作:

代码实现

import pandas as pd

# 定义输入数据
data = {
    'results': [
        {
            'id': 'knNyIzsECbl3YYPAKIQsEoaO4_roXDftV-auy9lSB-w',
            'publisher': {'name': 'Benzinga', 'homepage_url': 'https://www.benzinga.com/'},
            'title': 'Earnings Scheduled For May 11, 2021'
        },
        {
            'id': 'KNDx8p0PytFULh33UWse-BkT7XxpxLZtGLij22tiZMM',
            'publisher': {'name': 'The Motley Fool', 'homepage_url': 'https://www.fool.com/'},
            'title': 'Taysha Gene Therapies, Inc. (TSHA) Q1 2021 Earnings Call Transcript'
        }
    ]
}

# 筛选符合条件的条目并提取目标字段
target_data = []
for entry in data['results']:
    if entry['publisher']['name'] == 'Benzinga':
        target_data.append({
            'publisher': entry['publisher']['name'],
            'title': entry['title']
        })

# 转换为Pandas DataFrame
result_df = pd.DataFrame(target_data)
print(result_df)

输出结果

publisher                                      title
0  Benzinga           Earnings Scheduled For May 11, 2021

说明

  • 遍历results列表,通过判断publisher['name']筛选出目标条目
  • 对符合条件的条目,只提取需要的publisher名称和title字段,避免保留嵌套字典或多余结构
  • 最后将整理好的列表直接传入pd.DataFrame()生成目标结构的表格

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 23:45:42