Skip to content
Home » pandas Part 22 – Setting and Resetting Indices

pandas Part 22 – Setting and Resetting Indices

Spread the love

SETTING AND RESETTING INDICES

In the previous part of the Pandas series we were talking about sorting indices. Today we’ll be talking about another way of rearranging multi-indexed data, index setting and resetting. Let’s start with the latter. You can use the reset_index method if you want to turn index labels into columns. Let’s use the example from the previous article to demonstrate this:

In [2]:
import numpy as np
import pandas as pd

index = pd.MultiIndex.from_product([['Columbus', 'Seattle', 'Denver', 'Dallas'], [2019, 2020]])
data = pd.Series(np.random.rand(8), index=index)
data.index.names = ['city', 'year']
data
Out[2]:
city      year
Columbus  2019    0.109463
          2020    0.634263
Seattle   2019    0.079679
          2020    0.064878
Denver    2019    0.336507
          2020    0.358905
Dallas    2019    0.845219
          2020    0.185308
dtype: float64

And now let’s reset the index. This will produce a DataFrame with the index labels ‘city’ and ‘year’ turned into column names. We’ll also pass the name for the data column:

In [5]:
data_reset = data.reset_index(name='result')
data_reset
Out[5]:
city year result
0 Columbus 2019 0.109463
1 Columbus 2020 0.634263
2 Seattle 2019 0.079679
3 Seattle 2020 0.064878
4 Denver 2019 0.336507
5 Denver 2020 0.358905
6 Dallas 2019 0.845219
7 Dallas 2020 0.185308

This was resetting the index. You can also do the opposite. By setting the index you will turn a DataFrame into a multiply indexed DataFrame:

In [6]:
data_reset.set_index(['city', 'year'])
Out[6]:
result
city year
Columbus 2019 0.109463
2020 0.634263
Seattle 2019 0.079679
2020 0.064878
Denver 2019 0.336507
2020 0.358905
Dallas 2019 0.845219
2020 0.185308

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply