Checking a DataFrame's size
print(len(df)) # number of rows
print(df.shape) # (rows, columns)
Filtering rows with a condition
Putting a condition inside the square brackets keeps only the rows where it's true:
high_scorers = df[df["score"] > 70]
print(high_scorers)
df["score"] > 70 on its own produces a True/False Series, one value per row -- df[...] then keeps only the rows marked True.
Combining conditions
Use & for "and" and | for "or" -- each condition needs its own parentheses:
result = df[(df["score"] > 60) & (df["score"] < 90)]
Plain Python's and/or don't work here -- they're not defined for comparing a whole column at once, only &/| are.
Counting matches
A filtered DataFrame is still a DataFrame, so len() works the same way on it:
print(len(df[df["score"] > 70]))
Selecting specific columns
print(df[["name", "score"]]) # a list of column names keeps a DataFrame, not a Series
Why This Matters for Your Exams
df[condition] is the single most-used pattern in pandas -- almost every real question ("how many students scored above X," "which rows match Y") starts here. Mixing up &/and is the most common beginner error -- remember: & and | for pandas conditions, always with parentheses around each side.
Try it yourself
Real-time practice is a paid add-on
Reading units and the practice quiz are free. Pay once to unlock running real code for DATASCI.

