How to Search a Value Within a Pandas DataFrame Column?

This recipe helps you learn how to find a value in column within a Pandas Dataframe.

Objective For ‘Python Pandas Dataframe Search For Value In Column’

This step-by-step recipe will help you perform Python Pandas search for value in a dataframe.

Code Example For Python Dataframe Search for a Value in a Column

When working with a large dataset on any machine learning or data science project, there is a need to search for some values in a feature, and for that values, we need to get the values from other features.  Searching for values within a dataset might sound complicated, but Python Pandas makes it easy.

The Python Pandas Code below does the following:

  1. Creates data dictionary and converts it into DataFrame

  2. Uses the "where" function to filter out desired data columns. The pandas.DataFrame.where() function is like the if-then idiom, which checks for a condition to return the result accordingly.

Python Pandas Sample Code to Find Value in DataFrame Column

Below is the Python code to find value in column Pandas DataFrame-

Step 1 - Import the library

import pandas as pd

We have only imported the Python Pandas library needed for this code example.

Step 2 - Setting up the Data

We have created a dictionary of data and passed it to pd.DataFrame to make a dataframe with columns 'first_name', 'last_name', 'age', 'Comedy_Score' and 'Rating_Score'.

raw_data = {'first_name': ['Sheldon', 'Raj', 'Leonard', 'Howard', 'Amy'], 'last_name': ['Copper', 'Koothrappali', 'Hofstadter', 'Wolowitz', 'Fowler'], 'age': [42, 38, 36, 41, 35], 'Comedy_Score': [9, 7, 8, 8, 5], 'Rating_Score': [25, 25, 49, 62, 70]} df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age', 'Comedy_Score', 'Rating_Score']) print(df)

Try A Few More Pandas Code Examples With These Python Pandas Projects with Source Code

Step 3 - Using Python Pandas To Find in DataFrame The Desired Values

We are searching the data in the feature Rating_Score with values less than 50, and for those values, we are selecting the corresponding values in comedy_Score.

print(df['Comedy_Score'].where(df['Rating_Score'] < 50))

The output is as shown below -

 first_name     last_name  age  Comedy_Score  Rating_Score

0    Sheldon        Copper   42             9            25

1        Raj  Koothrappali   38             7            25

2    Leonard    Hofstadter   36             8            49

3     Howard      Wolowitz   41             8            62

4        Amy        Fowler   35             5            70

 

0    9.0

1    7.0

2    8.0

3    NaN

4    NaN

Name: Comedy_Score, dtype: float64

How To Search in a Pandas DataFrame Column For a Value Using Regular Expressions?

You can use the str.contains() method to perform Python Pandas search in a DataFrame column using regular expressions. For example, to search for all rows where the column name contains the letter ‘J’, you can use the following code-

 

df = pd.DataFrame({'name': ['John', 'Jane', 'Mike'], 'age': [25, 26, 27]})

filtered_df = df.loc[df['name'].str.contains('J')]

print(filtered_df)

 

The above code will give you the following output:

 name  age

0  John  25

1  Jane  26

 

Download Materials

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.