如何清洗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
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()
- Convert to lowercase with
Date Column:
- Retain original values since your expected output requires no changes to the date format.
Status Column:
- Simply convert to lowercase to maintain consistency (avoids stopword removal that would delete "no").
Number Column:
- Retain original values to preserve the
ioXXX-XXformat.
- Retain original values to preserve the
Result
Running this code will produce exactly the output you want:
| name | date | status | number |
|---|---|---|---|
| abcdef | 2022-07-11 | yes | io123-07 |
| ghijkl | 2022-07-12 | no | io456-08 |
内容的提问来源于stack exchange,提问作者rainy days.
相关产品推荐
相关产品推荐

