Book Image

Learning Pandas

By : Michael Heydt
Book Image

Learning Pandas

By: Michael Heydt

Overview of this book

Table of Contents (19 chapters)
Learning pandas
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Reindexing a Series


Reindexing in pandas is a process that makes the data in a Series or DataFrame match a given set of labels. This is core to the functionality of pandas as it enables label alignment across multiple objects, which may originally have different indexing schemes.

This process of performing a reindex includes the following steps:

  1. Reordering existing data to match a set of labels.

  2. Inserting NaN markers where no data exists for a label.

  3. Possibly, filling missing data for a label using some type of logic (defaulting to adding NaN values).

Here is a simple example of reindexing a Series. The following Series has an index with numerical values, and the index is modified to be alphabetic by simply assigning a list of characters to the .index property. This makes the values accessible via the character labels in the new index:

In [66]:
   # sample series of five items
   s = pd.Series(np.random.randn(5))
   s

Out[66]:
   0   -0.173215
   1    0.119209
   2   -1.044236
   3   -0.861849...