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

Build Customer Propensity to Purchase Model in Python
In this machine learning project, you will learn to build a machine learning model to estimate customer propensity to purchase.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

Data Analysis of Working Capital Management using Tableau
In this Data Analysis Project using Tableau, you will focus on optimizing working capital by analyzing receivables and payables data using Tableau and build actionable dashboards.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

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.

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.

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.