close

How to drop rows from pandas data frame that contains a particular string in a particular column?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to drop rows from pandas data frame that contains a particular string in a particular column in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

How to drop rows from pandas data frame that contains a particular string in a particular column?

  1. How to drop rows from pandas data frame that contains a particular string in a particular column?

    If your string constraint is not just one string you can drop those corresponding rows with:
    df = df[~df['your column'].isin(['list of strings'])]

  2. drop rows from pandas data frame that contains a particular string in a particular column

    If your string constraint is not just one string you can drop those corresponding rows with:
    df = df[~df['your column'].isin(['list of strings'])]

Method 1

pandas has vectorized string operations, so you can just filter out the rows that contain the string you don’t want:

In [91]: df = pd.DataFrame(dict(A=[5,3,5,6], C=["foo","bar","fooXYZbar", "bat"]))

In [92]: df
Out[92]:
   A          C
0  5        foo
1  3        bar
2  5  fooXYZbar
3  6        bat

In [93]: df[~df.C.str.contains("XYZ")]
Out[93]:
   A    C
0  5  foo
1  3  bar
3  6  bat

Method 2

If your string constraint is not just one string you can drop those corresponding rows with:

df = df[~df['your column'].isin(['list of strings'])]

The above will drop all rows containing elements of your list

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read