[986] Filter rows by containing a particular word in Pandas

alex_bn_lee / 2024-04-23 / 原文

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:

  1. 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.
  2. Inverting the Filter:

    • If you want to find rows that do not contain the string “ball,” you can use the negation operator (~):
      Python
      inverted_df = df[~df['ids'].str.contains("ball")]
    • The ~ negates the condition, resulting in rows where the ‘ids’ column does not contain “ball” 1.
  3. Partial Match and Remaining String:

    • If you want to work with partial matches and extract the remaining string after a partial match (e.g., grabbing the remaining part after ‘#’), you can use more complex string manipulation methods 1.