VOOZH about

URL: https://www.geeksforgeeks.org/python/get-all-rows-in-a-pandas-dataframe-containing-given-substring/

⇱ Get All Rows in a Pandas DataFrame Containing given Substring - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Get All Rows in a Pandas DataFrame Containing given Substring

Last Updated : 3 Oct, 2025

If you want to filter rows in a Pandas DataFrame based on whether a column contains a specific substring, you can use the str.contains() function. For example, you might want only rows where "Team" contains "Boston" or "Position" contains "PG".

For Example: This example shows how to select rows where a column contains "yes".


Output
 Answer
0 yes
3 yes

Explanation: Here we checked the "Answer" column and returned only rows containing "yes".

Syntax

DataFrame[DataFrame['column'].str.contains("substring")]

Parameters:

  • column: Column name where the search is applied.
  • substring: The text you want to match.

Return Value: A DataFrame with rows containing the given substring.

Examples

We will use the following DataFrame in all examples:

Output

Name Team Position College
0 Alex Boston Celtics PG MIT
1 Emily LA Lakers SG Stanford
2 Cathy Boston Celtics PG Harvard
3 David Chicago Bulls UG UC Berkeley
4 Eva Boston Celtics PG UC Davis

Example 1: This example filters rows where the "Position" column contains "PG".

Output

Name Team Position College
0 Alex Boston Celtics PG MIT
2 Cathy Boston Celtics PG Harvard
4 Eva Boston Celtics PG UC Davis

Example 2: This code selects rows where the "College" column contains "UC".

Output

Name Team Position College
3 David Chicago Bulls UG UC Berkeley
4 Eva Boston Celtics PG UC Davis

Example 3: This program filters rows that satisfy at least one of the conditions: "Team" contains "Boston" OR "College" contains "MIT".

Output

Name Team Position College
0 Alex Boston Celtics PG MIT
2 Cathy Boston Celtics PG Harvard
4 Eva Boston Celtics PG UC Davis

Explanation: The | operator means β€œOR”, so rows are included if either condition is true.

Comment