Find row with value in Pandas DataFrame

In this tutorial, we will learn how to find row/s with the desired value in Pandas DataFrame using Python.

Let’s first import all the necessary libraries and the dataset. I will be working on the dataset that I have made and is present on my computer. It is a small dataset, so you can verify the results in a short time.

import pandas as pd

df = pd.read_csv("data/Data.csv")
df

sample dataset for Python

Search in a specific column in Pandas DataFrame

Suppose you know the column name of the value you want to search, and now you want to look at all the rows with those values. In that case, we can use the function df.loc with certain logical conditions.
Let’s find all the rows that have the Country name France. We know it will be in the Country column, so using the logical operator ==. This will display only those rows which have France in the Country column. If France was also present in any other column, then it would not display those rows.

df.loc[df['Country']=='France']

Search in a specific column in Pandas DataFrame

Search in the entire Dataset

Suppose you don’t know the column name of the value you want to search for, or you want to search for a value irrespective of the Column name. In that case, we can use the function df.isin().
Let’s find all the rows in the dataset with the value Yes. We have to use axis = 1 to search in all dataset columns.

df[df.isin(["Yes"]).any(axis=1)]

Search in the entire Dataset

Leave a Reply

Your email address will not be published. Required fields are marked *