Programmer's Picnic · Python / Pandas · Lesson 1

Search, Select and Filter DataFrames

Find exactly the rows and values you need: by name, by score, by index position and by multiple conditions.

0 to infinity · Learn by making real DataFrames · No file required

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.

RowNameCityMarksFeePaid
0AshaVaranasi88True
1RaviRanchi42False
2MeeraVaranasi76True
3KabirPatna91False
Python · pandas
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.

Step 1

Select one column, or several

A single column name returns a Series. A list of column names returns a DataFrame.

Python · pandas
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.

Step 2

Find a student by an exact name

Build a condition for matching names. Pandas uses == to test equality.

Python · pandas
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).

Step 3

Select with .loc and .iloc

Use .loc for row and column labels; .iloc for integer positions.

Python · pandas
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.

Step 4

Search by a condition

Filter students who obtained at least 75 marks; choose only the columns relevant to the answer.

Python · pandas
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.

Step 5

Search with two conditions

Each comparison needs its own parentheses. Use & for AND; | for OR; ~ to reverse a condition.

Python · pandas
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 |.

Step 6

Find items in a list or a range

Use isin() to match several cities and between() for a numerical range.

Python · pandas
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.

Step 7

Search inside text

String methods help search text columns. Set regex=False for a literal substring and na=False to ignore missing names.

Python · pandas
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.

Step 8

Quickly count matching rows

Use a condition to count true values and see category frequencies.

Python · pandas
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
Python · pandas
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)  # Asha

Checkpoint

Can you find Ravi, select a single cell, find students with marks below 50 and locate students from either Varanasi or Ranchi?

Official guide to indexing and selecting.

← Previous lessonCourse indexNext lesson →