🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.loc[]

AI & DATA SCIENCE // df-loc

df.loc[] selects rows and columns by label — index names and column names — rather than by integer position.

Syntax

df.loc[row_labels, column_labels]

Deep Dive Course

loc[] is label-based indexing: selecting a single row label returns that row, selecting a column name returns that column, and slicing between two labels selects every row from the first label through the second, inclusive of both endpoints — unlike Python's normal slicing, and unlike iloc, where the end of a slice is always exclusive. loc[] also accepts a boolean array/condition for the row selector, making it the standard way to combine label-based and condition-based selection in a single call.

1Understanding df.loc[]

loc[] is label-based indexing: selecting a single row label returns that row, selecting a column name returns that column, and slicing between two labels selects every row from the first label through the second, inclusive of both endpoints — unlike Python's normal slicing, and unlike iloc, where the end of a slice is always exclusive. loc[] also accepts a boolean array/condition for the row selector, making it the standard way to combine label-based and condition-based selection in a single call.

💡

loc[]'s label-based slicing includes both endpoints, unlike Python's normal slice syntax and unlike iloc's position-based slicing, which both exclude the endpoint — a frequent source of off-by-one confusion.

editor.html
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob", "Carol"], "age": [30, 25, 35]}, index=["a", "b", "c"])
print(df.loc["b"])
localhost:3000

2Practical Example

Here is a real-world application of df.loc[] showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob", "Carol"], "age": [30, 25, 35]})
print(df.loc[df["age"] > 28, "name"])
localhost:3000

3Best Practices

Follow these guidelines when working with df.loc[]:

1. Use loc[] whenever you're selecting by meaningful labels, names, dates, IDs, rather than raw position

2. Remember loc[]'s slice endpoint is inclusive, unlike iloc[] and plain Python slicing, to avoid an off-by-one surprise

3. Combine loc[] with a boolean condition for the row selector to filter rows and select specific columns in one call

⚠️

Tip: loc[]'s label-based slicing includes both endpoints, unlike Python's normal slice syntax and unlike iloc's position-based slicing, which both exclude the endpoint — a frequent source of off-by-one confusion.

editor.html
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob", "Carol"], "age": [30, 25, 35]}, index=["a", "b", "c"])
print(df.loc["b"])
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob", "Carol"], "age": [30, 25, 35]}, index=["a", "b", "c"])
print(df.loc["b"])
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob", "Carol"], "age": [30, 25, 35]})
print(df.loc[df["age"] > 28, "name"])

Best Practices

  • Use loc[] whenever you're selecting by meaningful labels, names, dates, IDs, rather than raw position
  • Remember loc[]'s slice endpoint is inclusive, unlike iloc[] and plain Python slicing, to avoid an off-by-one surprise
  • Combine loc[] with a boolean condition for the row selector to filter rows and select specific columns in one call

Interview Question

Why does a loc[] label slice from 'a' to 'c' include the row labeled 'c', while a plain Python list slice like lst[0:2] excludes the element at index 2?

Hint: Think about loc[]'s deliberate design choice around label-based slicing.

loc[]'s slicing is label-based, not position-based, and pandas deliberately made label-slice endpoints inclusive, since with labels, there's no clean, universal concept of 'one before the end label' the way there is with integer positions — including both endpoints is simply more intuitive and predictable when you're specifying a range by name rather than by count. Plain Python slicing, and pandas' own position-based iloc[], both use the exclusive-end convention that comes from counting positions, where 'up to but not including' naturally makes sense.

Exercises

MediumPractice using df.loc[] in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob", "Carol"], "age": [30, 25, 35]}, index=["a", "b", "c"])
print(df.loc["b"])

Frequently Asked Questions

Why does a loc[] label slice from 'a' to 'c' include the row labeled 'c', while a plain Python list slice like lst[0:2] excludes the element at index 2?

loc[]'s slicing is label-based, not position-based, and pandas deliberately made label-slice endpoints inclusive, since with labels, there's no clean, universal concept of 'one before the end label' the way there is with integer positions — including both endpoints is simply more intuitive and predictable when you're specifying a range by name rather than by count. Plain Python slicing, and pandas' own position-based iloc[], both use the exclusive-end convention that comes from counting positions, where 'up to but not including' naturally makes sense.

Related Functions

df-ilocdf-atboolean-indexing