如何基于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=Truedrops 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:
- Markdown Tables: Use
pd.DataFrame.to_markdown()to generate a ready-to-paste Markdown table:
Which outputs a neatly formatted table like:print(result.to_markdown(index=False))dpl_Description w/o months dpl_Order Duration Charity X 36 No X 9 No Y 23 No - Reproducible Code Snippet: Share a list of lists/dictionaries that others can load directly into a DataFrame, avoiding manual formatting work.
内容的提问来源于stack exchange,提问作者ylux
相关产品推荐
相关产品推荐

