Skip to main content

5  Row Index Values

In this chapter we will focus on index operations, including changing and resetting the index.

5.1 Setting the Index

Every DataFrame has a default index values, but we can set our own.

We might do this in practice when one of the columns in the dataset contains values which uniquely identify each row in the dataset.

For example, this products dataset has an “id” column that uniquely represents each row, so we could set that column as the index:

from pandas import read_csv

request_url = "https://raw.githubusercontent.com/prof-rossetti/intro-to-python/main/data/products.csv"
df = read_csv(request_url)
df.head()
id name aisle department price
0 1 Chocolate Sandwich Cookies cookies cakes snacks 3.50
1 2 All-Seasons Salt spices seasonings pantry 4.99
2 3 Robust Golden Unsweetened Oolong Tea tea beverages 2.49
3 4 Smart Ones Classic Favorites Mini Rigatoni Wit... frozen meals frozen 6.99
4 5 Green Chile Anytime Sauce marinades meat preparation pantry 7.99

Setting the “id” column as the index:

# set the index and name it:
df.index = df["id"]
df.index.name = "id"

# drop old column, as necessary and desired:
df.drop(columns=["id"], inplace=True)

df.head()
name aisle department price
id
1 Chocolate Sandwich Cookies cookies cakes snacks 3.50
2 All-Seasons Salt spices seasonings pantry 4.99
3 Robust Golden Unsweetened Oolong Tea tea beverages 2.49
4 Smart Ones Classic Favorites Mini Rigatoni Wit... frozen meals frozen 6.99
5 Green Chile Anytime Sauce marinades meat preparation pantry 7.99

5.2 Resetting the Index

If you want to revert a DataFrame to use the default index values (auto-incrementing integers), we can use the reset method:

df.reset_index(inplace=True)
df.head()
id name aisle department price
0 1 Chocolate Sandwich Cookies cookies cakes snacks 3.50
1 2 All-Seasons Salt spices seasonings pantry 4.99
2 3 Robust Golden Unsweetened Oolong Tea tea beverages 2.49
3 4 Smart Ones Classic Favorites Mini Rigatoni Wit... frozen meals frozen 6.99
4 5 Green Chile Anytime Sauce marinades meat preparation pantry 7.99

5.3 Resetting a Multi-Index

If our dataset has a multi-index (like a composite primary key comprised of multiple columns), we can convert the index components to their own columns, and reset the index to be simpler.

Consider this dataset of time-series stock prices from the yahooquery package, where we have a row per stock per date:

from yahooquery import Ticker

symbols = ["MSFT", "AAPL", "GOOGL"]
ticker = Ticker(symbols)
df = ticker.history(start="2024-01-01", end="2024-01-05", adj_ohlc=True)
df
open high low volume close
symbol date
MSFT 2024-01-02 365.339695 367.333211 358.411280 25258600 362.417847
2024-01-03 360.600262 364.753404 360.111657 23083500 362.154022
2024-01-04 362.222390 364.597002 358.802155 20901500 359.554596
AAPL 2024-01-02 184.755430 186.028933 181.537146 82488700 183.264755
2024-01-03 181.862933 183.501697 181.083032 58414500 181.892548
2024-01-04 179.819409 180.747384 178.565669 71983600 179.582489
GOOGL 2024-01-02 137.244038 138.135548 135.193542 23711200 136.867615
2024-01-03 135.956278 138.313849 135.787882 24212100 137.610535
2024-01-04 137.115249 137.848280 135.064769 27137700 135.104385

We see the row index is a gnarly MultiIndex consisting of two values, representing both the “symbol” and the “date”:

df.index
MultiIndex([( 'MSFT', 2024-01-02),
            ( 'MSFT', 2024-01-03),
            ( 'MSFT', 2024-01-04),
            ( 'AAPL', 2024-01-02),
            ( 'AAPL', 2024-01-03),
            ( 'AAPL', 2024-01-04),
            ('GOOGL', 2024-01-02),
            ('GOOGL', 2024-01-03),
            ('GOOGL', 2024-01-04)],
           names=['symbol', 'date'])

Resetting the index:

# moving multi-index to their own separate columns:
df["symbol"] = df.index.get_level_values(0)
df["date"] = df.index.get_level_values(1)

# resetting the index:
df.reset_index(drop=True, inplace=True)
df.head()
open high low volume close symbol date
0 365.339695 367.333211 358.411280 25258600 362.417847 MSFT 2024-01-02
1 360.600262 364.753404 360.111657 23083500 362.154022 MSFT 2024-01-03
2 362.222390 364.597002 358.802155 20901500 359.554596 MSFT 2024-01-04
3 184.755430 186.028933 181.537146 82488700 183.264755 AAPL 2024-01-02
4 181.862933 183.501697 181.083032 58414500 181.892548 AAPL 2024-01-03

This gives us a flatter structure which may be easier to work with.