![]() |
VOOZH | about |
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".
Answer 0 yes 3 yes
Explanation: Here we checked the "Answer" column and returned only rows containing "yes".
DataFrame[DataFrame['column'].str.contains("substring")]
Parameters:
Return Value: A DataFrame with rows containing the given substring.
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.