Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to search a value within a Pandas DataFrame row?
Pandas DataFrame is a two-dimensional data structure that represents data in tabular form with rows and columns. Python provides several built-in methods like eq(), any(), loc[], and apply() to search for specific values within DataFrame rows.
Basic Value Search in a Column
The simplest approach is to search for a value in a specific column using boolean indexing ?
import pandas as pd
# Create a DataFrame
data = {'Name': ['Bhavish', 'Abhinabh', 'Siddhu'],
'Age': [25, 32, 28]}
df = pd.DataFrame(data)
# Search for a value in the 'Name' column
search_value = 'Siddhu'
result = df[df['Name'] == search_value]
print(result)
Name Age
2 Siddhu 28
Search Across All Columns Using eq() and any()
To search for a value across all columns in a DataFrame, combine eq() with any(axis=1) ?
import pandas as pd
# Create a DataFrame
data = {'Name': ['A', 'X', 'C', 'F', 'L', 'G'],
'Age': [25, 32, 18, 29, 13, 19]}
df = pd.DataFrame(data)
# Search for a value across all columns
search_value = 'F'
result = df[df.eq(search_value).any(axis=1)]
print(result)
Name Age 3 F 29
Search Within Specific Column Range Using loc[]
Use loc[] to search within a specific range of columns ?
import pandas as pd
# Create a DataFrame
data = {'Name': ['Shyam', 'Ranjan', 'Mohan', 'Raju', 'Dheeraj'],
'Age': [25, 32, 18, 22, 26],
'Designation': ['SDE', 'Tester', 'Web Developer', 'Intern', 'HR'],
'Salary': [50000, 17000, 26000, 20000, 17000]}
df = pd.DataFrame(data)
# Search for a value within a range of columns
search_value = 17000
result = df.loc[df.loc[:, 'Age':'Salary'].eq(search_value).any(axis=1)]
print(result)
Name Age Designation Salary
1 Ranjan 32 Tester 17000
4 Dheeraj 26 HR 17000
Search Using apply() and Lambda
The apply() function with a lambda expression provides flexible row-wise searching ?
import pandas as pd
# Create a simple DataFrame
data = {"Name": ["Keshav", "Vishal", "Aman", "Shubham", "Amar"],
"Age": [23, 22, 24, 26, 21]}
df = pd.DataFrame(data)
# Search for the value 22 in all rows using apply() and lambda
search_value = 22
if df.apply(lambda row: search_value in row.values, axis=1).any():
print(f"{search_value} found in at least one row")
else:
print(f"{search_value} not found in any row")
22 found in at least one row
Method Comparison
| Method | Best For | Performance |
|---|---|---|
Boolean indexing df[df['col'] == value]
|
Single column search | Fastest |
eq().any(axis=1) |
Search across all columns | Good |
loc[] with range |
Search within column subset | Good |
apply() with lambda |
Complex search conditions | Slower but flexible |
Conclusion
Use boolean indexing for simple column searches, eq().any(axis=1) for searching across all columns, and apply() with lambda for complex conditions. Choose the method based on your specific search requirements and performance needs.
