How to filter for a string on a Pandas DataFrame using Python

1 min

One easy way to check for cells that contains a specific text is to use the Pandas.Series.str.contains() method.

Let's dive into the example

The example

# Import the Pandas library
import pandas as pd

# We create our example dataframe
df = pd.DataFrame({"countries" :  ["Germany", "USA", "Germany", "France"]})

# We print the rows that contains "Ger"
print(df[df["countries"].str.contains("Ger")])
How to filter for a string on a pandas DataFrame using python

As you can see we filter for countries that contain the string Ger.

Here you are! You now know how to filter for a string on Pandas DataFrame using Python.

More on DataFrames

If you want to know more about DataFrame and Pandas. Check out the other articles I wrote on the topic, just here :

Pandas - The Python You Need
We gathered the only Python essentials that you will probably ever need.