Monday, September 1, 2025
HomeLanguagesSearch A pandas Column For A Value

Search A pandas Column For A Value

Prerequisites: pandas

In this article let’s discuss how to search data frame for a given specific value using pandas.

Function used

  • where() -is used to check a data frame for one or more condition and return the result accordingly. By default, The rows not satisfying the condition are filled with NaN value.
  • dropna() -This method allows the user to analyze and drop Rows/Columns with Null values. In this article it is used to deal with the cases where the rows that will have value as NaN because they will not satisfy a certain condition. 

Approach

  • Import modules
  • Create data
  • Traverse through the column looking for a specific value
  • If matched, select

There is a basic difference between selecting a specific values and selecting rows that have a specific value. For the later case the indices to be retrieved has to be stored in a list. Implementation of both cases is included in this article:

Data frame in use:

Example 1: Select tuple containing salary as 200

Python3




import pandas as pd
x = pd.DataFrame([["A", 100, "D"], ["B", 200, "E"], ["C", 100, "F"]],
                 columns=["Name", "Salary", "Department"])
  
# Searching in whole column
for i in range(len(x.Name)):
    if 200 == x.Salary[i]:
          
        # indx will store the tuple having that 
        # particular value in column.
        indx = i
  
# below line will print that tuple
x.iloc[indx]


Output:

Example 2: Search for people having salary of 100 and store the output in a dataframe again. 

Python3




import pandas as pd
x = pd.DataFrame([["A", 100, "D"], ["B", 200, "E"], ["C", 100, "F"]], 
                 columns=[ "Name", "Salary", "Department"])
  
# initialize the indx as a list
indx = []
  
# Searching in whole column
for i in range(len(x.Name)):
    if 100 == x.Salary[i]:
        
        # indx will store all the tuples having 
        # that particular value in column.
        indx.append(i)
  
# Final Dataframe having tuples
df = pd.DataFrame()
  
# this will append all tuples to the final
# dataframe.
for indexes in indx:
    df = df.append(x.iloc[indexes])
  
df = x.where(x.Salary == 100)
  
# It will remove NaN rows.
df.dropna()


Output:

RELATED ARTICLES

Most Popular

Dominic
32251 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6619 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11841 POSTS0 COMMENTS
Shaida Kate Naidoo
6735 POSTS0 COMMENTS
Ted Musemwa
7016 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6707 POSTS0 COMMENTS