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

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Classification Projects on Machine Learning for Beginners - 2
Learn to implement various ensemble techniques to predict license status for a given business.

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

Build an AI Chatbot from Scratch using Keras Sequential Model
In this NLP Project, you will learn how to build an AI Chatbot from Scratch using Keras Sequential Model.

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.