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

如何在使用iterrows遍历Pandas DataFrame时跳过指定行

问题分析与解决方案

你当前代码的核心问题是:直接用row == dish_one这类比较会返回一个布尔值Series(逐元素比较结果),而if语句需要单个布尔值,导致跳过逻辑完全不生效——这三行根本不会被跳过。下面是两种可行的修正方案:

方案1:通过索引判断(推荐,效率更高)

既然已经明确要跳过的行的位置,直接判断循环的index是否在目标集合里即可,集合查找是O(1)操作,速度更快:

# 把要跳过的索引存成集合,查找效率更高
skip_indices = {235, 621, 831}
dish_one = unimp_features.iloc[235]
dish_two = unimp_features.iloc[621]
dish_three = unimp_features.iloc[831]

for index, row in unimp_features.iterrows():
    if index in skip_indices:
        continue
    # 执行余弦相似度计算
    df_unimportant.loc[index, 'cos_one'] = 1 - spatial.distance.cosine(dish_one, row)
    df_unimportant.loc[index, 'cos_two'] = 1 - spatial.distance.cosine(dish_two, row)
    df_unimportant.loc[index, 'cos_three'] = 1 - spatial.distance.cosine(dish_three, row)

方案2:通过行内容判断(适用于索引不固定的场景)

如果必须通过行内容匹配来跳过,要用Series.equals()方法——它会比较整个行的所有元素是否完全一致,返回单个布尔值:

dish_one = unimp_features.iloc[235]
dish_two = unimp_features.iloc[621]
dish_three = unimp_features.iloc[831]

for index, row in unimp_features.iterrows():
    if row.equals(dish_one) or row.equals(dish_two) or row.equals(dish_three):
        continue
    # 执行余弦相似度计算
    df_unimportant.loc[index, 'cos_one'] = 1 - spatial.distance.cosine(dish_one, row)
    df_unimportant.loc[index, 'cos_two'] = 1 - spatial.distance.cosine(dish_two, row)
    df_unimportant.loc[index, 'cos_three'] = 1 - spatial.distance.cosine(dish_three, row)

进阶优化:避免使用iterrows(大数据量场景)

iterrows遍历效率较低,若DataFrame数据量大,建议用Pandas向量化操作替代:

skip_indices = [235, 621, 831]
dish_one = unimp_features.iloc[235]
dish_two = unimp_features.iloc[621]
dish_three = unimp_features.iloc[831]

# 先过滤掉要跳过的行
filtered_rows = unimp_features.drop(skip_indices)

# 批量计算并赋值
df_unimportant.loc[filtered_rows.index, 'cos_one'] = 1 - filtered_rows.apply(lambda x: spatial.distance.cosine(dish_one, x), axis=1)
df_unimportant.loc[filtered_rows.index, 'cos_two'] = 1 - filtered_rows.apply(lambda x: spatial.distance.cosine(dish_two, x), axis=1)
df_unimportant.loc[filtered_rows.index, 'cos_three'] = 1 - filtered_rows.apply(lambda x: spatial.distance.cosine(dish_three, x), axis=1)

内容的提问来源于stack exchange,提问作者Roald Andre Kvarv

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 07:50:55