By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
You’re working on a real-world data pipeline—maybe cleaning a 10GB CSV from a client, debugging a legacy ETL script, or building a dashboard that filters millions of rows in real time. Selecting, filtering, and indexing are the core operations that let you extract, transform, and analyze data efficiently.
.loc
.iloc
Real-world scenario:You inherit a script that processes sales data. The original dev used hardcoded row numbers (df[10:20]) to extract a subset. Now, the data format changed, and the script silently fails—returning wrong results instead of crashing. Your job: Rewrite it using .loc and boolean masks so it works even if the data shifts.
df[10:20]
0,1,2,...
python df.loc["row_label", "column_name"] # Single cell df.loc[["row1", "row2"], ["col1", "col2"]] # Multiple rows/columns
python df.iloc[0, 0] # First row, first column df.iloc[1:5, 2:4] # Rows 1-4, columns 2-3
.apply()
python mask = df["sales"] > 100 # Returns True/False for each row df[mask] # Only rows where sales > 100
[]
df[df["col"] > 0]["new_col"] = 5
python df[df["age"] > 30]["status"] = "senior" # ❌ Won't work!
python df.loc[df["age"] > 30, "status"] = "senior" # ✅ Correct
.at
.iat
df.at["row1", "col1"]
.query()
WHERE
python df.query("age > 30 & sales > 100") # Equivalent to df[(df["age"] > 30) & (df["sales"] > 100)]
.isin()
df["col"].isin([1, 2, 3])
OR
df["col"] == 1 | df["col"] == 2
python df[df["country"].isin(["USA", "Canada"])] # Only rows where country is USA or Canada
.where()
NaN
0
python df.where(df["sales"] > 100, 0) # Replace sales <= 100 with 0
pandas
pip install pandas
sales_data.csv
["date", "product", "region", "sales", "profit"]
You need to: 1. Load the data.2. Filter rows where sales > 1000 and region == "West".3. Update profit to profit * 1.1 for these rows (10% bonus).4. Save the filtered data to a new CSV.
sales > 1000
region == "West"
profit
profit * 1.1
import pandas as pd df = pd.read_csv("sales_data.csv") print(df.head()) # Verify data loaded correctly
mask = (df["sales"] > 1000) & (df["region"] == "West")
# ✅ Correct: Uses .loc to modify in-place df.loc[mask, "profit"] *= 1.1
filtered_df = df[mask] # Only rows matching the condition filtered_df.to_csv("west_high_sales.csv", index=False)
print(filtered_df.head()) # Check first few rows print(f"Total rows: {len(filtered_df)}") # Should match expected count
df.reset_index(drop=True)
.copy()
SettingWithCopyWarning
python subset = df[df["sales"] > 1000].copy() # ✅ Safe
python df["sales"].isna().sum() # Count NaN values
.info()
df["date"]
python print(f"Filtering {len(df)} rows with condition: sales > 1000")
df.loc[df["col"] > 0, "new_col"] = 5
df["col1"] > 0 & df["col2"] == 1
TypeError
&
and
(df["col1"] > 0) & (df["col2"] == 1)
subset = df[df["col"] > 0].copy()
df.reset_index(drop=True, inplace=True)
"Which method would you use to select the first 5 rows, regardless of index?" Answer: .iloc (position-based).
Boolean masks:
"How would you filter rows where sales > 100 and region == "West"?" Answer: df[(df["sales"] > 100) & (df["region"] == "West")].
sales > 100
df[(df["sales"] > 100) & (df["region"] == "West")]
Chained indexing trap:
"What’s wrong with this code: df[df["age"] > 30]["status"] = "senior"?" Answer: It creates a temporary copy and fails silently. Use .loc instead.
df[df["age"] > 30]["status"] = "senior"
Performance:
df[df["col"].isin([1, 2, 3])]
df[(df["col"] == 1) | (df["col"] == 2) | (df["col"] == 3)]
if
False
You have a DataFrame df with columns ["name", "age", "score"]. Write a one-liner to: 1. Filter rows where age > 25 and score > 80.2. Update the score column to score * 1.05 for these rows.
df
["name", "age", "score"]
age > 25
score > 80
score
score * 1.05
df.loc[(df["age"] > 25) & (df["score"] > 80), "score"] *= 1.05
(df["age"] > 25) & (df["score"] > 80)
score *= 1.05
df.loc["row_label", "col_name"]
df.iloc[0, 0]
df[df["col"] > 100]
df[(df["col1"] > 0) & (df["col2"] == 1)]
df.loc[mask, "col"] = new_value
df.query("age > 30 & score > 80")
df.where(df["col"] > 100, 0)
df.iat[0, 0]
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.