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

如何将Pandas DataFrame中列表格式的行展开为多行

Pandas 拆分列表类型列为多行(保留对应日期)

针对你当前DataFrame中point_id和aggregated_flow列为列表格式的情况,无需复杂循环,直接使用Pandas的explode()方法就能高效完成拆分,实现每个列表元素对应单独一行且保留原日期:

解决方案代码

# 对需要拆分的列表列执行explode,同时重置索引
flow_df = flow_df.explode(['point_id', 'aggregated_flow'], ignore_index=True)

说明

  • explode()支持传入多个列名组成的列表,会同时将这些列的列表元素拆分为独立行,且自动匹配同一行的对应元素(比如[7,8,9]和[2.353,55.51,28.792]会一一对应拆分)
  • ignore_index=True会重置拆分后的DataFrame索引,避免出现重复索引的问题

效果对比

原DataFrame:

date   point_id                  aggregated_flow
0  10/02/2022  [7, 8, 9]   [2.353, 55.51, 28.792]
1  10/01/2022  [7, 8, 9]  [6.281, 53.444, 23.371]

执行代码后得到的结果:

date  point_id aggregated_flow
0  10/02/2022         7           2.353
1  10/02/2022         8           55.51
2  10/02/2022         9          28.792
3  10/01/2022         7           6.281
4  10/01/2022         8          53.444
5  10/01/2022         9          23.371

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 18:55:35