Fundamentals 4 min read

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.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Delete Rows Containing Specific Text in Pandas (Python)

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythondata cleaningpandasrow deletionstring filtering
Python Crawling & Data Mining
Written by

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!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.