Row Indexing (loc, iloc, Boolean)

How do you confidently grab exactly the rows you need from a messy dataset, every single time, without accidentally including or excluding critical information? In data analysis, selecting the right subset of data is not just a preliminary step; it is a critical operation that directly impacts the accuracy and reliability of your insights. Incorrectly filtered data can lead to skewed analyses, flawed models, and poor business decisions. Pandas offers powerful, precise tools for this task, ensuring your data extractions are always on target.

Pinpointing Rows with loc (Label-Based)

The .loc[] accessor in Pandas is your primary tool for label-based indexing. This means you select data using the actual labels of your DataFrame's index and columns. When you use loc, you are explicitly telling Pandas to look for specific row labels and column names, making your selections highly readable and robust to changes in data order. It is particularly useful when your DataFrame has a meaningful, non-integer index, such as dates, product IDs, or geographical codes.

pythonSelecting Rows and Columns by Label with `loc`
pythonInclusive Slicing with `loc`
Try It Yourself
Using the df DataFrame from the previous example, select all rows from 'P003' to 'P005' (inclusive) and only the 'Product' and 'Category' columns. Print the resulting DataFrame.
python

Selecting by Position with iloc (Integer-Based)

The .iloc[] accessor enables integer-position-based indexing. This method treats your DataFrame as a grid, similar to how you would index elements in a Python list or NumPy array. You specify row and column selections using their 0-based integer positions. This is particularly useful when you need to select data based on its absolute position, regardless of the actual index labels, or when working with DataFrames that have a default integer index.

pythonSelecting Rows and Columns by Integer Position with `iloc`
pythonExclusive Slicing with `iloc`
Check Your Understanding
When slicing a DataFrame df with df.loc['start_label':'end_label'] and df.iloc[start_pos:end_pos], what is the key difference in how the end point is handled?
Try It Yourself
Using the df DataFrame, select rows at integer positions 0, 2, and 4. From these selected rows, extract the columns at integer positions 0 (Product) and 1 (Category). Print the resulting DataFrame.
python

Filtering Data with Boolean Conditions

Boolean indexing, also known as Boolean filtering, offers a highly flexible way to select rows based on their values. Instead of specifying labels or positions, you provide a Series of True/False values to the DataFrame's indexing operator []. Pandas then returns only the rows where the corresponding Boolean value is True. This method is exceptionally powerful for filtering data based on complex conditions, allowing you to extract subsets that meet specific criteria across one or more columns.

pythonBoolean Indexing with a Single Condition
pythonCombining Multiple Boolean Conditions
Check Your Understanding
Which operator is correctly used to combine two Boolean conditions for filtering a Pandas DataFrame?
Try It Yourself
Using the df DataFrame, filter for products that are either 'Electronics' AND 'In_Stock', OR are 'Audio' products. Print the resulting DataFrame.
python

Choosing the Right Tool: loc, iloc, or Boolean?

Comparing Pandas Row Selection Methods
Featureloc (Label-Based)iloc (Integer-Position-Based)Boolean Indexing (Conditional)
Selection TypeBy explicit row/column labelsBy 0-based integer positionsBy True/False conditions
InputLabels (strings, numbers, dates)Integers (0, 1, 2...)Boolean Series/array
Slicing BehaviorInclusive of end label (df.loc['A':'C'] includes 'C')Exclusive of end position (df.iloc[0:3] excludes position 3)N/A (no direct slicing, filters based on condition)
Common Use CaseSelecting specific known rows/columns by name, date rangesSelecting first/last N rows, rows at specific positions, samplingFiltering data based on value criteria (e.g., price > 100, category == 'Electronics')
Error HandlingRaises KeyError if label not foundRaises IndexError if position out of boundsRaises ValueError if Boolean Series length mismatch
A quick guide to help you decide which Pandas indexing method to use.
Key Takeaways
  • loc selects data using explicit row and column labels, offering clear, readable selections.

  • iloc selects data using 0-based integer positions for rows and columns, similar to Python list indexing.

  • Slicing with loc is inclusive of the end label, while slicing with iloc is exclusive of the end position.

  • Boolean indexing filters rows based on conditions, returning only rows where the condition evaluates to True.

  • Combine multiple Boolean conditions using & (AND) and | (OR) operators, always enclosing each condition in parentheses.

  • Mastering these indexing methods provides precise control over your data, ensuring accuracy and integrity in every selection.

← All lessons in Pandas Basics

Ready to keep this from fading?

Bitelrn turns lessons like this into a full course — quizzes, a knowledge map, and spaced review.

Get started free