Loading content...
Loading content...
Master data slicing in Pandas: select specific columns, extract rows via label-based loc and positional iloc, create Boolean filter masks with &, |, and ~, use isin(), and combine row filtering with column projection.
A corporate sales database contains millions of transactions spanning years. An analyst rarely studies the entire raw table at once. Instead, business questions demand precision:
Isolating specific variables or index positions (e.g. keeping only Product and Sales while dropping 40 irrelevant columns).
Retaining only records that satisfy analytical criteria (e.g. Sales >= 50000 or Region == "North").
Pandas provides two column selection modes based on bracket count:
Given an inventory table with Product, Category, Price, and Sales. Complete the 3 column projection tasks:
loc vs. ilocRow selection requires choosing between label coordinates and integer memory positions:
df.loc[0] finds the row labeled 0df.loc[0:2] includes rows 0, 1, and 2!df.iloc[0] finds the first physical rowdf.iloc[0:2] returns rows 0 and 1 (standard Python rules).Use iloc and loc on an employee table:
Filtering in Pandas works in two connected phases:
&, |, and ~When combining conditions, standard Python and/or keywords fail. You must use:
(df["Sales"] > 50000) & (df["Age"] < 25).Filter the department records below:
df.locIn data analytics, you often need to filter rows and project specific columns simultaneously:
Perform 5 targeted queries on employee performance:
Employee and Salary columnsSalary >= 55000Performance >= 90isin()Employee and Performance for employees with Performance >= 85Test your understanding of loc, iloc, Boolean masks, and multi-condition subsetting:
Test your understanding with real-world query prediction and syntax questions.
What is the primary difference between df.loc and df.iloc in Pandas?