LambdaLab
Informatics Practices · Class 12 · Python Pandas · Series
PandasSeries⏱️ 6 min read

Reindexing a Series

Reindexing is a technique for creating a brand-new Series from an existing one — the new Series has the same kind of labels, but arranged in the order you specify.

1Reorder the labels

reindex() takes a list of index labels and returns a new Series whose rows follow that exact order. Explore what happens when you reorder, reverse, or ask for a label that doesn't exist:

Reindex explorer

Reindexing builds a BRAND-NEW Series with the labels you ask for, in the order you ask for. Ask for a label that doesn't exist and its value is NaN.

s2 = s1.reindex(['b', 'd', 'c', 'a', 'e'])
s1 (original)
ahi
bhello
cbye
dok
ehappy
s2 (reindexed)
bhello
dok
cbye
ahi
ehappy

Same values, new order — and s1 itself never changes. reindex() always returns a new Series.

s2 = s1.reindex(['b', 'd', 'c', 'a', 'e'])

2Reordering existing labels

Pass the labels in whatever order you want. The values travel with their labels, and the original Series is left untouched.

reindex.py

3Asking for a label that doesn't exist

If you list a label that isn't in the original Series, Pandas still creates that row — but it has no value to put there, so it fills it with NaN (which also makes the Series float64 if it was numeric).

reindex_nan.py
Tip
Want a different filler than NaN? Pass fill_value=0 (or any value): marks.reindex(['a', 'z'], fill_value=0).
Key Takeaway
reindex() always returns a new Series in the order you asked for. Labels that don't exist come back as NaN (or your fill_value).
Quick Check

s has labels a, b, c. What does s.reindex(['c', 'a']) return?

Quick Check

You reindex a numeric Series with a label that doesn't exist. That row's value is…