Index and MultiIndex

Pandas Basics

2 min read

Published Sep 29 2025, updated Oct 24 2025


21
0
0
0

PandasPython

What is an Index?

  • Every Pandas Series or DataFrame has an index.
  • It labels rows, allowing you to access, slice, and align data.
  • By default, a DataFrame gets a RangeIndex (0, 1, 2, …).

Example:

import pandas as pd

df = pd.DataFrame({
    "Name": ["Alice", "Bob", "Charlie"],
    "Age": [25, 30, 35]
})

print(df)

Output:

      Name Age
0 Alice 25
1 Bob 30
2 Charlie 35

0,1,2 is the default index.




Custom Index

  • You can set a column as the index using .set_index():
df.set_index("Name", inplace=True)
print(df)

Output:

         Age
Name
Alice 25
Bob 30
Charlie 35

Now the index is Name, which can be used for label-based access:

df.loc["Bob"]

Output:

Age 30
Name: Bob, dtype: int64




Index Properties and Methods

Attribute / Method

Description

df.index

Returns the index object

df.columns

Returns column labels (also an Index object)

df.reset_index()

Resets the index to default RangeIndex, optionally keeping old index as column

df.set_index("col")

Sets a column as index

df.sort_index()

Sorts rows by index labels

df.index.name

Set or get the name of the index

df.index.values

Get array of index values

df.index.is_unique

Check if index has unique labels






MultiIndex (Hierarchical Index)

  • MultiIndex allows multiple levels of indexing in rows or columns.
  • Useful for grouped, hierarchical, or panel-like data.
  • MultiIndex works well with .groupby(), .pivot_table(), and aggregation.


Creating MultiIndex - from arrays

arrays = [
    ["East", "East", "West", "West"],
    ["Store1", "Store2", "Store1", "Store2"]
]

index = pd.MultiIndex.from_arrays(arrays, names=("Region", "Store"))

df = pd.DataFrame({
    "Sales": [100, 150, 200, 250]
}, index=index)

print(df)

Output:

               Sales
Region Store
East Store1 100
       Store2 150
West Store1 200
       Store2 250

Region is level 0, Store is level 1.





Creating MultiIndex - from tuples

tuples = [("East", "Store1"), ("East", "Store2"), ("West", "Store1"), ("West", "Store2")]
index = pd.MultiIndex.from_tuples(tuples, names=("Region", "Store"))

df = pd.DataFrame({"Sales": [100,150,200,250]}, index=index)



Accessing MultiIndex data

Single level:

df.loc["East"]

Output:

        Sales
Store
Store1 100
Store2 150


Specific row:

df.loc[("East","Store2")]

Output:

Sales 150
Name: (East, Store2), dtype: int64


Slicing levels:

df.loc[pd.IndexSlice["East":"West", "Store1":"Store2"], :]



Resetting and swapping levels

# moves MultiIndex back to columns
df.reset_index()

 # swaps level 0 and level 1
df.swaplevel()




Sorting MultiIndex

# sort by first level
df.sort_index(level=0)

# sort by second level, then first
df.sort_index(level=[1,0])

Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact