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

如何将含records字段的嵌套字典转换为Pandas DataFrame?

字典转DataFrame的正确方法

你当前的代码直接传入整个data字典,会把外层的'records'当作列名,对应的列值是包含三个字典的列表,这显然不是你想要的结果。

正确的做法是提取字典里'records'对应的列表,再传入DataFrame构造方法:

方法一:使用from_records

import pandas as pd

data = {'records': [{'centre_contact_no': 1578},
                    {'centre_contact_no': 7517},
                    {'centre_contact_no': 3590}
                   ]
       }

df = pd.DataFrame.from_records(data['records'])

方法二:直接用DataFrame构造函数

因为data['records']本身就是符合要求的字典列表,直接传入即可:

df = pd.DataFrame(data['records'])

两种方法都会生成如下结构的DataFrame:

centre_contact_no
1578
7517
3590

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 13:15:38