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

如何修复Pandas中apply函数的IndexError?基于sum_result多条件映射的函数优化

Fix IndexError in Pandas Custom Function When No Rows Have sum_result=2

Let's break down the issues in your code and fix them step by step.

Why the IndexError Happens

The error pops up because when there are no rows where sum_result=2, df.loc[df['sum_result']==2,'frame'] returns an empty Series. Trying to access .iloc[0] or .iloc[-1] on an empty Series triggers that "out-of-bounds" error.

Your current code also has a few other problems:

  • You're looping through the entire DataFrame inside the apply function, which is inefficient (since apply already iterates row by row)
  • You're using bitwise operators (&, |) instead of logical operators (and, or) for boolean checks
  • The frame difference logic is flawed—you need to compare the current row's frame to all sum_result=2 frames, not just the first/last, and check if any difference is ≤10

Fixed Solution

First, precompute the frame values where sum_result=2 once (outside the apply function) to avoid redundant calculations. Then explicitly handle the case where this list is empty.

Here's the corrected code:

# Precompute the list of frame values where sum_result equals 2
icv_frames = df.loc[df['sum_result'] == 2, 'frame'].tolist()

def result_func(row):
    sum_val = row['sum_result']
    frame_val = row['frame']
    
    if sum_val == 2:
        return 'ICV'
    elif sum_val == 1:
        # Only check frame differences if there are ICV frames
        if icv_frames:
            # Verify if any frame difference is within the 10 threshold
            if any(abs(frame_val - icv_frame) <= 10 for icv_frame in icv_frames):
                return 'ReviewNG'
        # Default to 'Other' if no ICV frames or difference is too large
        return 'Other'
    elif sum_val == 0:
        return 'Other'
    else:
        return ""

# Apply the function to each row (axis=1 passes entire rows to the function)
df['Result'] = df.apply(result_func, axis=1)

Key Improvements

  1. Precompute ICV Frames: We calculate icv_frames once instead of every row, which boosts efficiency.
  2. Empty ICV Frame Handling: We check if icv_frames is non-empty before accessing its values, eliminating the IndexError.
  3. Correct Logical Checks: Use any() to confirm if any frame difference meets the ≤10 condition, and use proper logical operators instead of bitwise ones.
  4. Full Row Access: Using apply(..., axis=1) lets us access both sum_result and frame for each row—your original code only passed sum_result, which broke the frame comparison logic.

Test the Fixed Code

Running this with your sample dataset will produce your expected output:

frameuser1user2sum_resultResult
0000Other
1100Other
2201ReviewNG
3312ICV
4411ReviewNG
5500Other

If there are no rows with sum_result=2, the function will skip the ReviewNG check for sum_result=1 rows and return 'Other' instead, without throwing any errors.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 04:42:42