Search A pandas Column For A Value
Last Updated :
17 Dec, 2020
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" ])
for i in range ( len (x.Name)):
if 200 = = x.Salary[i]:
indx = i
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" ])
indx = []
for i in range ( len (x.Name)):
if 100 = = x.Salary[i]:
indx.append(i)
df = pd.DataFrame()
for indexes in indx:
df = df.append(x.iloc[indexes])
df = x.where(x.Salary = = 100 )
df.dropna()
|
Output:
Share your thoughts in the comments
Please Login to comment...