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

如何清洗Pandas DataFrame并保留日期、'No'及指定字符?

Solution: Column-Specific Data Cleaning

Your original code applies a generic text cleaning pipeline to all columns, which is why it's breaking Date, Status, and Number columns. The fix is to apply targeted cleaning rules to each column instead of using a one-size-fits-all function.

Key Issues in Original Code

  • Overly broad punctuation removal: Removes hyphens (critical for Date and Number formats).
  • Removal of digit-containing words: Deletes entire Date values and trims Number columns.
  • Stopword filtering: Would remove "no" from Status (since "no" is a stopword in NLTK).
  • Uniform cleaning for all columns: Columns like Date and Number don't need text stripping or special character removal.

Corrected Code

import pandas as pd

# Load your CSV data (replace with your actual file path)
df = pd.read_csv('your_file.csv')

# Clean Name column: lowercase + remove non-alphabetic characters
df['name'] = df['Name'].str.lower().str.replace(r'[^a-z]', '', regex=True).str.strip()

# Keep Date column unchanged
df['date'] = df['Date']

# Clean Status column: just convert to lowercase
df['status'] = df['Status'].str.lower()

# Keep Number column unchanged
df['number'] = df['Number']

# Reorder columns to match your expected output
df = df[['name', 'date', 'status', 'number']]

# Verify the result
print(df)

Explanation of Each Step

  1. Name Column:

    • Convert to lowercase with str.lower()
    • Remove all non-alphabetic characters using regex [^a-z] (keeps only letters)
    • Strip any leftover whitespace with str.strip()
  2. Date Column:

    • Retain original values since your expected output requires no changes to the date format.
  3. Status Column:

    • Simply convert to lowercase to maintain consistency (avoids stopword removal that would delete "no").
  4. Number Column:

    • Retain original values to preserve the ioXXX-XX format.

Result

Running this code will produce exactly the output you want:

namedatestatusnumber
abcdef2022-07-11yesio123-07
ghijkl2022-07-12noio456-08

内容的提问来源于stack exchange,提问作者rainy days.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 11:40:39