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

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

Build a Customer Churn Prediction Model using Decision Trees
Develop a customer churn prediction model using decision tree machine learning algorithms and data science on streaming service data.

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

Build a Face Recognition System in Python using FaceNet
In this deep learning project, you will build your own face recognition system in Python using OpenCV and FaceNet by extracting features from an image of a person's face.

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.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.