In Pandas, you can filter rows based on whether a specific column contains a particular word or substring. Here are a few ways to achieve this:
-
Using
str.contains()
:- To filter rows where a specific column (let’s say ‘ids’) contains the word “ball,” you can use the following code:
Python
filtered_df = df[df['ids'].str.contains("ball")]
- This will return rows where the ‘ids’ column contains the substring “ball” 1.
- To filter rows where a specific column (let’s say ‘ids’) contains the word “ball,” you can use the following code:
Python
-
Inverting the Filter:
- If you want to find rows that do not contain the string “ball,” you can use the negation operator (
~
): Pythoninverted_df = df[~df['ids'].str.contains("ball")]
- The
~
negates the condition, resulting in rows where the ‘ids’ column does not contain “ball” 1.
- If you want to find rows that do not contain the string “ball,” you can use the negation operator (
-
Partial Match and Remaining String: