Eliminating Rows Containing a Specific String
Basically, this function will search for the string in the given column and return the rows respective to that. For this, we need to create a new data frame by filtering the data frame using this function.
Syntax:
df[ df[ “column” ].str.contains( “someString” )==False ]
Creating a Sample Pandas DataFrame
Here, we will create a sample DataFrame that we will use in further examples.
Python3
# Importing the library import pandas as pd # Dataframe df = pd.DataFrame({ 'team' : [ 'Team 1' , 'Team 1' , 'Team 2' , 'Team 3' , 'Team 2' , 'Team 3' ], 'Subject' : [ 'Math' , 'Science' , 'Science' , 'Math' , 'Science' , 'Math' ], 'points' : [ 10 , 8 , 10 , 6 , 6 , 5 ]}) # display df |
Output:
team Subject points
0 Team 1 Math 10
1 Team 1 Science 8
2 Team 2 Science 10
3 Team 3 Math 6
4 Team 2 Science 6
5 Team 3 Math 5
How to Drop Rows that Contain a Specific String in Pandas?
In Pandas, we can drop rows from a DataFrame that contain a specific string in a particular column. In this article, we are going to see how to drop rows that contain a specific string in Pandas.