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

如何使用Pandas根据指定frame_id列表补全缺失行并填充0值

Pandas补全缺失frame_id记录解法

你可以通过set_index+reindex的组合快速实现需求,逻辑简洁高效,后续代码如下:

# 将frame_id设置为索引
df = df.set_index("frame_id")
# 按目标frame_id列表重索引,缺失的行自动填充0
df = df.reindex(frame_ids, fill_value=0)
# 将frame_id从索引恢复为普通列,得到最终DataFrame结果
df = df.reset_index()

# 如果需要转为你示例要求的字典列表格式,补充下面这行即可
res = df.to_dict("records")

如果偏好链式写法可以合并为一行:

res = df.set_index("frame_id").reindex(frame_ids, fill_value=0).reset_index().to_dict("records")

运行后得到的res和你给出的预期结果完全一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 05:06:00