How to Delete Rows Containing Specific Text in Pandas (Python)
This article explains how to use pandas in Python to remove rows where a column contains a given keyword or multiple keywords, providing concise code examples and practical tips for handling such data‑cleaning tasks.
1. Introduction
Hello, I am PiPi. A question was raised in a Python community about deleting rows that contain a specific value, such as the word "电力" (electricity), using pandas.
2. Basic Solution
The following code removes rows where the column Column1 contains the string "电力":
# Delete rows in Column1 that contain '电力'
df = df[~df['Column1'].str.contains('电力')]3. Extending to Multiple Keywords
To delete rows that contain either "电力" or "电梯", combine the patterns with the | operator:
df = df[~df['col1'].str.contains('电力|电梯')]This approach can be extended to any number of keywords by separating them with |.
4. Summary
The provided pandas snippets demonstrate how to filter out unwanted rows based on textual content, offering a simple and effective method for data cleaning in Python.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
