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

如何基于DataFrame列唯一值高效迭代行并筛选数据?

Efficient Pandas Solution for Filtering Product Rows by Duration Criteria

Let's fix that slow loop with a fully vectorized approach—this will handle your 60k rows in a fraction of the time, no per-product loops needed.

Core Solution Code

import pandas as pd

# First, create a flag for each row indicating if its product has a 12-month entry
has_12_month = result.groupby('dpl_Description w/o months')['dpl_Order Duration'].transform(
    lambda x: (x == 12).any()
)

# Build the filter condition:
# - Keep all rows if the product doesn't have a 12-month entry, OR
# - If the product does have a 12-month entry, keep rows where Charity is "Yes" OR duration is 12/24/36
filter_condition = (
    ~has_12_month 
    | (result['Charity'] == "Yes") 
    | (result['dpl_Order Duration'].isin([12, 24, 36]))
)

# Apply the filter
result_filtered = result[filter_condition].reset_index(drop=True)

Why This Is Way Faster

Your original loops are slow because:

  • They repeatedly slice and modify the original DataFrame (inplace=True drops are expensive, especially in loops)
  • Iterating over unique products and then individual rows/durations forces Pandas to work against its optimized vectorized design

This approach uses Pandas' built-in groupby.transform to create a flag for every row in one pass, then applies a single vectorized filter. All operations happen in bulk, leveraging Pandas' C-optimized backend instead of Python-level loops.

Verification with Your Sample Data

For your sample dataset (assuming all Charity values are "No"), this code will:

  • Keep all rows for products X, F, Z, C (none have a 12-month entry)
  • For product Y (which has a 12-month entry), only keep the row with duration 12 (since other Y rows don't meet the duration criteria and aren't charity entries)
    This matches exactly your expected output.

Side Note: Better Ways to Share Tabular Data

For Stack Overflow or similar platforms, here are cleaner options:

  1. Markdown Tables: Use pd.DataFrame.to_markdown() to generate a ready-to-paste Markdown table:
    print(result.to_markdown(index=False))
    
    Which outputs a neatly formatted table like:
    dpl_Description w/o monthsdpl_Order DurationCharity
    X36No
    X9No
    Y23No
  2. Reproducible Code Snippet: Share a list of lists/dictionaries that others can load directly into a DataFrame, avoiding manual formatting work.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 08:03:11