How to find rows/elements that have a specific value in Pandas

Huda
3 min readJan 14, 2021

--

Photo by Miguel Á. Padriñán from Pexels

There are several ways to retrieve certain rows or elements with a specific value from Pandas data frame.

We start by importing and read the data frame, I’ve used COVID 19 data from the worldmeter.info website

Here are a few of the best methods to extract specific values.

1. Filtering by using the equality operator (==) with the column name

From our data frame, let’s look for the rows that contain “North America” using the equality operator as in the following code:

We can also look for a specific country inside North America’s region by using the and operator (&), as in the following code:

2. Filtering using the equality operator (==) with df.values without specifying the column name

We can also look at multiple values using df.values as in the code below:

3. Filtering by using .isin( ) method

Since the .isin( ) method takes a list, we can check multiple values from the same column, as in the following code:

4. Using the .str.contains( ) method, which can be used only with string

Note:This method won’t work if we have null values, so we have to clean the data first.

5. Mapping using .map function which is used to apply a function to all elements of a sequence.

In this case, we will use .map function in the data frame to pass a lambda function to the Region column

--

--

Huda

Data Scientist with recent experience in data acquisition and data modeling, statistical analysis, machine learning, deep learning and NLP