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

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS

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.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.