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
Selected Reading
Python - Search DataFrame for a specific value with pandas
Searching a DataFrame for specific values is a common operation in data analysis. Pandas provides several methods to locate and retrieve rows based on specific criteria using boolean indexing and iloc.
Creating the DataFrame
First, let's create a sample DataFrame with car information ?
import pandas as pd
# Creating DataFrame with car data
dataFrame = pd.DataFrame({
"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],
"Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],
"Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000],
"Units_Sold": [100, 120, 150, 110, 200, 250]
})
print("DataFrame:")
print(dataFrame)
DataFrame:
Car Cubic_Capacity Reg_Price Units_Sold
0 BMW 2000 7000 100
1 Lexus 1800 1500 120
2 Tesla 1500 5000 150
3 Mustang 2500 8000 110
4 Mercedes 2200 9000 200
5 Jaguar 3000 6000 250
Method 1: Using Boolean Indexing
The most efficient way to search for specific values is using boolean indexing ?
import pandas as pd
dataFrame = pd.DataFrame({
"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],
"Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],
"Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000],
"Units_Sold": [100, 120, 150, 110, 200, 250]
})
# Search for car with Registration Price 5000
result = dataFrame[dataFrame['Reg_Price'] == 5000]
print("Car with Registration Price 5000:")
print(result)
Car with Registration Price 5000:
Car Cubic_Capacity Reg_Price Units_Sold
2 Tesla 1500 5000 150
Method 2: Using Loop with iloc
You can also use a loop to find the index and then use iloc to retrieve the row ?
import pandas as pd
dataFrame = pd.DataFrame({
"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],
"Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],
"Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000],
"Units_Sold": [100, 120, 150, 110, 200, 250]
})
# Search for car with Registration Price 5000 using loop
for i in range(len(dataFrame)):
if 5000 == dataFrame.iloc[i]['Reg_Price']:
indx = i
break
# Display the found value
print("Found car at index", indx)
print(dataFrame.iloc[indx])
Found car at index 2 Car Tesla Cubic_Capacity 1500 Reg_Price 5000 Units_Sold 150 Name: 2, dtype: object
Searching Multiple Values
You can search for multiple conditions using logical operators ?
import pandas as pd
dataFrame = pd.DataFrame({
"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],
"Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],
"Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000],
"Units_Sold": [100, 120, 150, 110, 200, 250]
})
# Search for cars with price greater than 6000 and units sold greater than 120
result = dataFrame[(dataFrame['Reg_Price'] > 6000) & (dataFrame['Units_Sold'] > 120)]
print("Cars with price > 6000 and units sold > 120:")
print(result)
Cars with price > 6000 and units sold > 120:
Car Cubic_Capacity Reg_Price Units_Sold
2 Tesla 1500 5000 150
4 Mercedes 2200 9000 200
5 Jaguar 3000 6000 250
Comparison
| Method | Performance | Readability | Best For |
|---|---|---|---|
| Boolean Indexing | Fast | High | Single/multiple conditions |
| Loop with iloc | Slow | Medium | Complex logic |
| query() method | Medium | High | String-based conditions |
Conclusion
Boolean indexing is the most efficient method for searching DataFrames. Use iloc with loops only when you need the specific index position or complex conditional logic.
Advertisements
