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

遍历DataFrame创建新列:获取列表索引值遇问题求助

解决Pandas根据列值获取列表索引并高效生成新列的问题

问题原因分析

  • itertuples遍历赋值错误:每次循环中df['Index_value'] = Year.index(value)是对整个列赋值,最后一次循环的结果会覆盖所有行,导致所有索引值都是最后一个元素的索引。
  • 直接赋值报错:list.index()仅支持单个标量值,而df['Year']是Series对象,无法直接传入,因此触发“Series的真值不明确”错误。

高效解决方案(适配大数据集)

方案1:字典映射(推荐,性能最优)

先将Year列表转换为“值-索引”的映射字典,再通过map方法完成向量化映射,这是处理大规模数据最快的方式:

import pandas as pd

Year = [2020,2021,2022]
# 构建值到索引的映射字典
year_index_map = {year: idx for idx, year in enumerate(Year)}

df = pd.DataFrame({'Year':[2020,2021,2021,2022],'Sales':[100000,101000,103000,112000]})
# 生成新列
df['Index_value'] = df['Year'].map(year_index_map)

print(df)

输出结果:

Year   Sales  Index_value
0  2020  100000            0
1  2021  101000            1
2  2021  103000            1
3  2022  112000            2

方案2:apply方法(实现简单,性能略逊于字典映射)

通过apply对Year列的每个元素单独调用list.index():

df['Index_value'] = df['Year'].apply(lambda x: Year.index(x))

注意:如果df['Year']存在不在Year列表中的值,会抛出ValueError,可以添加默认值处理:

df['Index_value'] = df['Year'].apply(lambda x: Year.index(x) if x in Year else -1)

性能说明

避免使用itertuples/iterrows等行循环方式,这类方法在大数据集下效率极低。Pandas的向量化操作(如map、优化后的apply)底层基于C实现,性能远高于Python层面的循环。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 00:05:56