How to search a value within a Pandas DataFrame row?
Last Updated :
01 Dec, 2021
In this article, we will see how to search a value within Pandas DataFrame row in Python.
Importing Libraries and Data
Here we are going to import the required module and then read the data file as dataframe.
The link to dataset used is here
Python3
import pandas as pd
df = pd.read_csv( "data.csv" )
|
Output:
Searching a Value
Here we will search the column name with in the dataframe.
Syntax : df[df[‘column_name’] == value_you_are_looking_for]
where df is our dataFrame
We will search all rows which have a value “Yes” in purchased column.
Python3
df[df[ "Purchased" ] = = "Yes" ]
|
Output:
We can also use more than one condition to search a value. Lets see a example to find all rows which have Age value between 35 and 40 inclusive.
Syntax : df[condition]
where df is our dataFrame
Python3
df[(df[ "Age" ] > = 35 ) & (df[ "Age" ] < = 40 )]
|
Output:
Share your thoughts in the comments
Please Login to comment...