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

如何优化DataFrame中基于元组列表批量赋值的低效代码?

优化pandas逐元素赋值的低效问题

你的问题核心在于用Python循环逐元素操作pandas对象,这完全违背了pandas的设计初衷——pandas是为向量化操作优化的,Python循环的开销加上逐元素的内部检查会让速度变得极慢。另外你用list_val.index(i)来获取子列表的索引,每次都会遍历整个list_val,这也是一个隐形的性能杀手。

下面是两种高效的优化方案,核心思路都是先把数据整理成适合pandas处理的结构,再利用内置的向量化函数完成转换:

方案一:构建长格式数据后转宽格式(推荐)

这种方法先把所有需要赋值的行、列、值信息收集成一个统一的列表,再通过pivot直接生成目标DataFrame,完全避免循环赋值:

import pandas as pd

# 示例数据
list_val = [ [(0, 0.9), (451, 0.5)] , [(10, 0.5), (52, 0.2)] ]
an_other_list = [10, 1000, 1002, 1003] + list(range(451, 455)) + list(range(52, 55))

# 收集所有行索引、列索引、对应值的三元组
records = []
for col_pos, sub_tuples in enumerate(list_val):
    # 获取当前子列表对应的列名(来自an_other_list的对应位置)
    col_name = an_other_list[col_pos]
    for row_idx_in_other, val in sub_tuples:
        # 获取当前元组对应的行索引(来自an_other_list的指定位置)
        row_name = an_other_list[row_idx_in_other]
        records.append( (row_name, col_name, val) )

# 转成长格式DataFrame
df_long = pd.DataFrame(records, columns=["row_index", "col_index", "value"])

# 转成你需要的宽格式DataFrame
df = df_long.pivot(index="row_index", columns="col_index", values="value")

# 如果需要把an_other_list的所有元素都作为行索引(空值显示NaN),可以加上reindex
df = df.reindex(an_other_list)

方案二:用字典预先构建列数据

如果你更习惯按列处理,可以先为每一列构建一个字典(键是行索引,值是对应的数据),再直接生成DataFrame:

import pandas as pd

list_val = [ [(0, 0.9), (451, 0.5)] , [(10, 0.5), (52, 0.2)] ]
an_other_list = [10, 1000, 1002, 1003] + list(range(451, 455)) + list(range(52, 55))

# 构建列数据字典:键是列名,值是{行索引: 值}的字典
col_data = {}
for col_pos, sub_tuples in enumerate(list_val):
    col_name = an_other_list[col_pos]
    col_data[col_name] = {an_other_list[row_idx]: val for row_idx, val in sub_tuples}

# 生成DataFrame,自动对齐行索引
df = pd.DataFrame(col_data).reindex(an_other_list)

为什么原来的代码慢?

  1. list_val.index(i)的低效:每次调用都会遍历整个list_val寻找匹配项,时间复杂度是O(n),当list_val规模大时,这会带来巨大的额外开销。用enumerate直接获取索引是O(1)的操作。
  2. 逐元素赋值的开销:set_value(现在已被废弃,推荐用.at/.iat)每次操作都要经过pandas的内部校验和索引查找,Python循环的叠加会让这些开销被放大,而向量化操作是用C实现的,速度快几个数量级。

内容的提问来源于stack exchange,提问作者agreüs

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:29:12