Source Code:
(back to article)
import pandas as pd # Create a sample DataFrame df = pd.DataFrame({'A': ['foo', 'bar', 'baz'], 'B': [1, 2, 3]}) # Define the substring you want to filter by substring = 'ba' # Use the `str.contains()` method to filter the DataFrame by substring filtered_df = df[df['A'].str.contains(substring)] print(filtered_df)
Result:
Report an issue