orient='dict'返回每列对应一个字典(键为索引、值为单元格),orient='list'返回每列对应一个列表;orient='records'按行转字典列表;orient='index'易因非字符串或重复索引引发KeyError;NaN需手动转None以防JSON序列化失败;大DF中orient='records'性能最差。

orient='dict' 和 orient='list' 最常用,但含义容易搞反
很多人以为 orient='dict' 是把 DataFrame 变成“字典”,orient='list' 是变成“列表”——其实完全相反:orient='dict' 的结果是每个列对应一个字典(键是索引,值是单元格),而 orient='list' 才是每个列对应一个 Python 列表。
实际用法取决于你后续怎么取数:要按列名快速查某行所有字段?选 orient='records';要按索引查某列所有值?orient='dict' 更直接。
-
orient='dict'→ 返回{'col_a': {0: val1, 1: val2}, 'col_b': {0: val3, 1: val4}} -
orient='list'→ 返回{'col_a': [val1, val2], 'col_b': [val3, val4]} -
orient='records'→ 返回[{'col_a': val1, 'col_b': val3}, {'col_a': val2, 'col_b': val4}]
orient='index' 容易报 KeyError,尤其索引不是字符串时
orient='index' 把索引变成外层 key,列名变成内层 key。问题在于:如果原始 DataFrame 索引是整数(比如默认 RangeIndex),转出的字典 key 就是数字,但很多人下意识当字符串用,一查就 KeyError。
更隐蔽的是:如果索引含重复值,_to_dict() 不报错,但后出现的行会覆盖前面同索引的值,数据直接丢。
立即学习“Python免费学习笔记(深入)”;
- 检查索引类型:
df.index.dtype,非字符串索引建议先df.index = df.index.astype(str) - 检查索引唯一性:
df.index.is_unique,不唯一时慎用orient='index' - 示例错误:
result[0]报错?试试result[0]→ 实际可能是result['0']或根本不存在
NaN 值在不同 orient 下表现不一致,JSON 序列化前必须处理
to_dict() 对 NaN 的处理不是统一的:orient='list' 和 orient='records' 里 NaN 变成 None,但 orient='dict' 里仍是 nan(float 类型),这会导致后续 json.dumps() 直接报 TypeError: Object of type float32 is not JSON serializable。
别指望 pandas 自动转,它只管结构不管序列化兼容性。
- 统一转
None:用df = df.where(pd.notna(df), None)再调to_dict() - 或用
df.fillna(float('nan')).replace({float('nan'): None}).to_dict(...) - 注意:
fillna(None)在某些 pandas 版本中不生效,必须用where或replace
大 DataFrame 调用 to_dict() 性能跳变,orient='records' 最慢
同样一个 10 万行 × 10 列的 DataFrame,orient='list' 耗时约 80ms,orient='dict' 约 95ms,但 orient='records' 可能飙到 400ms 以上——因为它是逐行构造 dict,Python 层开销大,且每行都新建 dict 对象。
如果你只是想遍历数据,别急着全量转字典;如果最终要 JSON,df.to_json(orient='records') 比 to_dict().to_json() 快 3–5 倍。
- 性能排序(快→慢):
list≈dictsplit index records - 内存占用:orient='records' 生成最多新对象,GC 压力明显
- 替代方案:用
df.itertuples(index=False)或df.values.tolist()+ 列名 zip,更快更省内存










