Skip to content

Indexing and Selecting Data (loc, iloc)

Why selection matters

Most Pandas bugs come from:

  • Selecting the wrong rows
  • Accidentally getting a copy instead of a view
  • Confusing label-based vs position-based indexing

Pandas provides two primary tools:

  • locloc โ†’ label-based
  • ilociloc โ†’ position-based

Setup example DataFrame

Sample DataFrame
import pandas as pd
 
df = pd.DataFrame(
    {
        "name": ["Asha", "Ravi", "Meera", "Zoya"],
        "age": [23, 28, 26, 31],
        "city": ["Pune", "Delhi", "Delhi", "Pune"],
        "score": [88, 91, 77, 95],
    },
    index=["u1", "u2", "u3", "u4"],
)
 
print(df)
Sample DataFrame
import pandas as pd
 
df = pd.DataFrame(
    {
        "name": ["Asha", "Ravi", "Meera", "Zoya"],
        "age": [23, 28, 26, 31],
        "city": ["Pune", "Delhi", "Delhi", "Pune"],
        "score": [88, 91, 77, 95],
    },
    index=["u1", "u2", "u3", "u4"],
)
 
print(df)

Selecting columns

One column
print(df["age"])  # returns a Series
One column
print(df["age"])  # returns a Series
Multiple columns
print(df[["name", "score"]])
Multiple columns
print(df[["name", "score"]])

Selecting rows with locloc (labels)

loc by label
print(df.loc["u2"])          # one row (Series)
print(df.loc[["u1", "u4"]]) # multiple rows (DataFrame)
loc by label
print(df.loc["u2"])          # one row (Series)
print(df.loc[["u1", "u4"]]) # multiple rows (DataFrame)

Selecting rows + columns together:

loc rows + cols
print(df.loc[["u1", "u3"], ["name", "score"]])
loc rows + cols
print(df.loc[["u1", "u3"], ["name", "score"]])

Selecting rows with ilociloc (positions)

iloc by position
print(df.iloc[0])      # first row
print(df.iloc[0:2])    # first two rows
print(df.iloc[:, 0:2]) # first two cols
iloc by position
print(df.iloc[0])      # first row
print(df.iloc[0:2])    # first two rows
print(df.iloc[:, 0:2]) # first two cols

Boolean filtering (most common)

Filter rows
filtered = df[df["city"] == "Delhi"]
print(filtered)
Filter rows
filtered = df[df["city"] == "Delhi"]
print(filtered)

Combine conditions:

Multiple conditions
filtered = df[(df["city"] == "Pune") & (df["score"] >= 90)]
print(filtered)
Multiple conditions
filtered = df[(df["city"] == "Pune") & (df["score"] >= 90)]
print(filtered)

Safer assignment after filtering

When you filter and then assign, prefer .loc.loc:

Safe assignment with loc
df.loc[df["city"] == "Delhi", "is_delhi"] = True
 
df["is_delhi"] = df["is_delhi"].fillna(False)
print(df)
Safe assignment with loc
df.loc[df["city"] == "Delhi", "is_delhi"] = True
 
df["is_delhi"] = df["is_delhi"].fillna(False)
print(df)

Note on && and ||

In Pandas boolean filters:

  • Use && for AND, || for OR
  • Always wrap sub-conditions in parentheses

This avoids operator precedence bugs.

๐Ÿงช Try It Yourself

Exercise 1 โ€“ Create a DataFrame

Exercise 2 โ€“ Select a Column

Exercise 3 โ€“ Filter Rows

If this helped you, consider buying me a coffee โ˜•

Buy me a coffee

Was this page helpful?

Let us know how we did