如何移除DataFrame中数字与字符串共存条目并删除字母类词汇?
Hey there! Let's tackle your two filtering needs step by step, using your sample data and desired output as a guide. The goal here is to end up with only the rows where Text contains pure English phrases like "Main Road" and "Parallel Road".
First, let's clarify the requirements
Looking at your sample data and expected result, we need to:
- Remove any text segments that mix numbers with letters/symbols (like "12-43", "12g", "f12")
- Filter out rows that end up with empty or meaningless short terms (like "KG,JK" or "MG,JK") after cleaning
Step 1: Clean the Text column to strip out number-mixed content
We'll use regular expressions to target and remove the unwanted parts. Here's a custom cleaning function that handles all cases:
import pandas as pd import re def clean_text(text): # Remove any word that contains a number (e.g., 12-43, 12g, f12) no_num_words = re.sub(r'\b\w*\d\w*\b', '', text) # Strip out punctuation and extra spaces no_punct = re.sub(r'[^\w\s]', '', no_num_words).strip() # Remove short, meaningless terms (1-2 characters like KG, JK) no_short_terms = re.sub(r'\b\w{1,2}\b', '', no_punct).strip() # Collapse multiple spaces into one return re.sub(r'\s+', ' ', no_short_terms)
Apply this function to your DataFrame:
# Keep only the columns we care about matrix = matrix[['Text', 'Association']] # Add a cleaned text column matrix['Cleaned_Text'] = matrix['Text'].apply(clean_text)
Step 2: Filter out rows with empty cleaned text
Now we just need to keep rows where our cleaned text isn't empty:
# Filter and clean up the final DataFrame filtered_matrix = matrix[matrix['Cleaned_Text'] != ''].copy() # Replace the original Text column with cleaned content filtered_matrix['Text'] = filtered_matrix['Cleaned_Text'] # Drop the temporary column filtered_matrix = filtered_matrix.drop(columns=['Cleaned_Text'])
Full working example
Here's the complete code with sample data to test:
import pandas as pd import re # Sample data matching your input data = { 'Text': ["12-43 KG,JK", "12g MG,JK", "Main Road", "12-45 JK,TG", "f12 Parallel Road"], 'Association': ['A', 'B', 'C', 'D', 'E'] } matrix = pd.DataFrame(data) # Keep relevant columns matrix = matrix[['Text', 'Association']] def clean_text(text): no_num_words = re.sub(r'\b\w*\d\w*\b', '', text) no_punct = re.sub(r'[^\w\s]', '', no_num_words).strip() no_short_terms = re.sub(r'\b\w{1,2}\b', '', no_punct).strip() return re.sub(r'\s+', ' ', no_short_terms) matrix['Cleaned_Text'] = matrix['Text'].apply(clean_text) filtered_matrix = matrix[matrix['Cleaned_Text'] != ''].copy() filtered_matrix['Text'] = filtered_matrix['Cleaned_Text'] filtered_matrix = filtered_matrix.drop(columns=['Cleaned_Text']) print(filtered_matrix)
Running this will give you exactly the output you want:
Text Association 2 Main Road C 4 Parallel Road E
Breakdown of the regex patterns
\b\w*\d\w*\b: Targets any word that includes a number (the\bensures we match full words, not parts of longer ones)[^\w\s]: Removes all punctuation and non-word/non-space characters\b\w{1,2}\b: Gets rid of short 1-2 character terms that are likely meaningless abbreviations (like KG, JK)
Why your original code fell short
Your existing code only filters out rows with pure numeric text or single-character entries. It doesn't handle mixed number-letter content or meaningless short terms, which is why we need the regex-based cleaning approach for better precision.
内容的提问来源于stack exchange,提问作者aeapen

