Question:medium

Briefly explain the purpose of Boolean Indexing with respect to DataFrames. Support your answer with a suitable example.

Show Hint

Boolean indexing selects only those rows of a DataFrame for which a condition is True. Give a short example such as df[df['Marks'] > 80].
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Start with the idea:
A DataFrame has many rows, and we often want only some of them. Boolean indexing lets us describe the rows we want by a rule.
The rule produces True or False for every row, and Pandas keeps the True ones.

Step 2: Why it is useful:
It replaces a loop and an if statement with a single expression. The code is short and fast.
We can also join rules with & (and) and | (or).

Step 3: Example with a price column:
import pandas as pd
df = pd.DataFrame({'Item': ['Pen', 'Book', 'Bag'], 'Price': [10, 150, 400]})
print(df[df['Price'] >= 150])
The test df['Price'] >= 150 gives False, True, True for the three rows.
So the rows for Book and Bag are returned, and the row for Pen is dropped.

Final Answer:
Boolean indexing selects rows of a DataFrame that satisfy a condition. In the example, df[df['Price'] >= 150] returns the Book and Bag rows.
Was this answer helpful?
0