Open In App

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

Approach

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




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. 




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:


Article Tags :