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

如何用Pandas保留行仅清除指定列数据(CSV数据清洗)

Solution: Clear Specific Fields Instead of Dropping Rows in Pandas

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 .loc indexer 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 None ensures Pandas recognizes them as missing values, which we can format as NULL during 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 17:57:48