Search, Select and Filter DataFrames
Find exactly the rows and values you need: by name, by score, by index position and by multiple conditions.
Our sample data
All examples use the same students. Every code box begins with its own DataFrame, so you can copy and run it independently.
| Row | Name | City | Marks | FeePaid |
|---|---|---|---|---|
| 0 | Asha | Varanasi | 88 | True |
| 1 | Ravi | Ranchi | 42 | False |
| 2 | Meera | Varanasi | 76 | True |
| 3 | Kabir | Patna | 91 | False |
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
print(students)Vocabulary: Selecting a column is different from filtering rows. Searching often combines both.
Select one column, or several
A single column name returns a Series. A list of column names returns a DataFrame.
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
print(students["Name"])
print(students[["Name", "Marks"]])Remember: Use double square brackets for a DataFrame of multiple columns.
Find a student by an exact name
Build a condition for matching names. Pandas uses == to test equality.
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
match = students[students["Name"] == "Meera"]
print(match)Remember: For a match, compare every row: the result is a Boolean mask (True or False for each row).
Select with .loc and .iloc
Use .loc for row and column labels; .iloc for integer positions.
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
print(students.loc[1, "Name"]) # Ravi
print(students.loc[0:2, ["Name", "Marks"]])
print(students.iloc[1, 2]) # 42
print(students.iloc[0:2, 0:3])Remember: .loc[0:2] includes label 2; .iloc[0:2] stops before position 2.
Search by a condition
Filter students who obtained at least 75 marks; choose only the columns relevant to the answer.
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
high_scorers = students.loc[students["Marks"] >= 75, ["Name", "Marks"]]
print(high_scorers)Remember: Use <, <=, >, >=, == and != to compare values.
Search with two conditions
Each comparison needs its own parentheses. Use & for AND; | for OR; ~ to reverse a condition.
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
varanasi_top = students.loc[
(students["City"] == "Varanasi") & (students["Marks"] >= 75),
["Name", "City", "Marks"]
]
print(varanasi_top)
support_or_unpaid = students.loc[
(students["Marks"] < 50) | (~students["FeePaid"]),
["Name", "Marks", "FeePaid"]
]
print(support_or_unpaid)Remember: Do not use Python and/or directly between pandas Series; use & and |.
Find items in a list or a range
Use isin() to match several cities and between() for a numerical range.
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
print(students[students["City"].isin(["Ranchi", "Patna"])])
print(students[students["Marks"].between(70, 90)])Remember: between(70, 90) includes both 70 and 90 by default.
Search inside text
String methods help search text columns. Set regex=False for a literal substring and na=False to ignore missing names.
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
found = students.loc[
students["Name"].str.contains("a", case=False, regex=False, na=False),
["Name", "City"]
]
print(found)Remember: String searches can be case-sensitive; case=False makes this search ignore letter case.
Quickly count matching rows
Use a condition to count true values and see category frequencies.
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
print((students["Marks"] >= 75).sum())
print(students["City"].value_counts())
print(len(students))Remember: True counts as 1 when summed; value_counts gives the frequency of each city.
Practice B · Locate records
Display only Name and Marks for students from Varanasi with marks of at least 80.
Reveal a possible solution
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
answer = students.loc[
(students["City"] == "Varanasi") & (students["Marks"] >= 80),
["Name", "Marks"]
]
print(answer) # AshaCheckpoint
Can you find Ravi, select a single cell, find students with marks below 50 and locate students from either Varanasi or Ranchi?