如何用Pandas保留行仅清除指定列数据(CSV数据清洗)
Got it, let's adjust your script to keep the rows intact while only clearing the firstname, lastname, and email fields for matching customers. Here's a clean, efficient way to do it:
Step 1: Read the Opt-Out Customer List
First, make sure you've loaded the list of hashed customer IDs that need cleaning:
import pandas as pd # Read the opt-out customer CSV and extract the hashed_customer column as a list cust_opt_out_id = pd.read_csv('path/to/your/opt_out_customers.csv')['hashed_customer'].tolist()
Step 2: Target and Clear Specific Columns
Instead of filtering out rows, use Pandas' .loc indexer to directly update the relevant fields for matching customers. This is way more efficient than looping with iterrows() for large datasets:
# Locate rows where hashed_customer is in the opt-out list, then set specified columns to None df_in.loc[df_in['hashed_customer'].isin(cust_opt_out_id), ['firstname', 'lastname', 'email']] = None
Step 3: Export the Cleaned Data
When saving back to CSV, use na_rep='NULL' to ensure empty values show up as NULL (matching your desired output):
# Export the cleaned DataFrame to CSV df_in.to_csv('cleaned_customer_orders.csv', index=False, na_rep='NULL')
Why This Works
- The
.locindexer lets you target a subset of rows (matching opt-out customers) and a subset of columns (the personal info fields) in one go. - Setting values to
Noneensures Pandas recognizes them as missing values, which we can format asNULLduring export. - This avoids the inefficiency of looping through every row with
iterrows(), which is slow for large datasets.
What Was Wrong With the Original Script
Your original code df_cust_out = df_in[~df_in['hashed_eater_uuid'].isin(cust_opt_out_id)] was filtering out entire rows that matched the opt-out list. We don't want to remove the rows—just erase the personal data while keeping the rest of the order details.
内容的提问来源于stack exchange,提问作者JMV12

