How to filter for a string on a Pandas DataFrame using Python
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
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")])
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 :
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Related Articles
Continue your learning journey with these related topics
Master Data Science in Days, Not Months 🚀
Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚