Pandas Indexing Deep Dive

Data SciencePandasFree Lesson

Advertisement

Introduction

Advanced indexing techniques in Pandas for complex data selection.

MultiIndex

import pandas as pd

# Create multi-level index
arrays = [
    ["A", "A", "B", "B"],
    [1, 2, 1, 2]
]
index = pd.MultiIndex.from_arrays(arrays, names=["Group", "Number"])
df = pd.DataFrame({"Value": [10, 20, 30, 40]}, index=index)

# Indexing multiindex
df.loc["A"]
df.loc[("B", 1)]
df.xs("A", level="Group")

Query Method

df = pd.DataFrame({
    "A": [1, 2, 3, 4],
    "B": [10, 20, 30, 40],
    "C": ["x", "y", "z", "w"]
})

# SQL-like syntax
df.query("A > 2 and B < 40")
df.query("C == 'x' | C == 'y'")

Boolean Indexing

# Complex conditions
mask = (df["A"] > 1) & (df["B"] < 30)
df[mask]

# isin
df[df["C"].isin(["x", "y"])]

# between
df[df["A"].between(2, 3)]

Practice Problems

  1. Create and index MultiIndex DataFrame
  2. Use query with variables
  3. Slice MultiIndex levels
  4. Combine multiple conditions
  5. Use loc vs iloc effectively

Advertisement

Need Expert Python Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement