Python脚本字段过滤逻辑异常:需移除同时满足三条件的字段但当前逻辑不符预期
Let's break down what's going wrong here and fix it step by step. Your goal is to only remove records that meet all three conditions at the same time:
CreatedByiskoala(note: your code useskoala_with an underscore—double-check if that's intentional!)Bookis in{'PI','SI','II','OT','FG'}Categoryis in{'Cert','CertPlus','Cap','Downside'}
Your current code has reversed logic and includes an extra, unrequired condition (LinkageLinkType != 'B2B_COUNTER_TRADE'), which is why it's incorrectly removing records that should be kept.
What's Wrong with the Existing Logic?
The current if statement removes records when:
qr2.iloc[i]['LinkageLinkType'] != 'B2B_COUNTER_TRADE' \ and qr2.iloc[i]['CreatedBy'] == 'koala_' \ and qr2.iloc[i]['Book'] in {'PI','SI','II','OT','FG'} \ and qr2.iloc[i]['Category'] not in {'Cert','CertPlus','Cap','Downside'}
This is the opposite of your requirement: it's deleting records where Category isn't in your target set, while you want to delete records where Category is in the set (along with the other two conditions). The LinkageLinkType check is also unneeded unless it's a hidden requirement you didn't mention.
Fix 1: Correct the Loop-Based Logic
If you want to keep using your loop approach with list L, adjust the condition to target only the records that meet all three required criteria. Then remove those indices from L:
# Reset L to include all indices of qr2 first L = list(range(len(qr2))) for i in range(len(qr2)): # Check if ALL three conditions are met (this is the record we want to remove) if (qr2.iloc[i]['CreatedBy'] == 'koala_' # Verify if this should be 'koala' instead of 'koala_' and qr2.iloc[i]['Book'] in {'PI','SI','II','OT','FG'} and qr2.iloc[i]['Category'] in {'Cert','CertPlus','Cap','Downside'}): if i in L: L.remove(i) # Filter qr2 to keep only the remaining indices filtered_qr2 = qr2.iloc[L]
Fix 2: Use Pandas Boolean Indexing (Recommended)
Looping through DataFrames is inefficient and error-prone. A cleaner, faster approach is to use Pandas' built-in boolean filtering. We'll create a mask that identifies records to remove, then keep everything else:
# Define the conditions for records we want to REMOVE remove_mask = (qr2['CreatedBy'] == 'koala_' # Double-check the underscore here & qr2['Book'].isin({'PI','SI','II','OT','FG'}) & qr2['Category'].isin({'Cert','CertPlus','Cap','Downside'})) # Keep records that do NOT match the remove_mask filtered_qr2 = qr2[~remove_mask] # If you still need the L list of kept indices, you can get it like this: L = filtered_qr2.index.tolist()
Key Notes:
- Double-check the
CreatedByvalue: your code uses'koala_'(with an underscore) but your requirement specifies'koala'. Make sure these match to avoid missing records! - The original
LinkageLinkType != 'B2B_COUNTER_TRADE'condition was removed because it wasn't part of your stated requirement. If you do need to exclude records with thatLinkageLinkType, add it back to theremove_maskwith an&operator. - Boolean indexing is the standard Pandas way to filter data—it's faster and easier to read/maintain than looping through rows.
内容的提问来源于stack exchange,提问作者Paula Ciesielska

