Get all rows in a Pandas DataFrame containing given substring

Get all rows in a Pandas DataFrame containing given substring

If you want to filter rows in a pandas DataFrame based on whether a certain column contains a given substring, you can use the str.contains() method.

Here's how you can do this:

import pandas as pd # Sample DataFrame df = pd.DataFrame({ 'text': ['apple pie', 'banana split', 'cherry tart', 'apple turnover'] }) # Filter rows where 'text' column contains the substring 'apple' filtered_df = df[df['text'].str.contains('apple')] print(filtered_df) 

This will output:

 text 0 apple pie 3 apple turnover 

Note: The str.contains() method is case-sensitive by default. If you want to make it case-insensitive, you can use the case parameter:

filtered_df = df[df['text'].str.contains('apple', case=False)] 

If you want to check multiple columns for the substring, you can loop over those columns or use the apply method with a lambda function. Remember to handle NaN values appropriately when using string methods.


More Tags

encryption scenekit pushviewcontroller twitter mount reusability database-relations spring-cloud-feign advanced-rest-client android-camera2

More Programming Guides

Other Guides

More Programming Examples