Book Image

Learning pandas - Second Edition

By : Michael Heydt
Book Image

Learning pandas - Second Edition

By: Michael Heydt

Overview of this book

You will learn how to use pandas to perform data analysis in Python. You will start with an overview of data analysis and iteratively progress from modeling data, to accessing data from remote sources, performing numeric and statistical analysis, through indexing and performing aggregate analysis, and finally to visualizing statistical data and applying pandas to finance. With the knowledge you gain from this book, you will quickly learn pandas and how it can empower you in the exciting world of data manipulation, analysis and science.
Table of Contents (16 chapters)

Re-indexing a Series

Re-indexing in pandas is a process that makes the data in a Series conform to a set of labels. It is used by pandas to perform much of the alignment process and is hence a fundamental operation.

Re-indexing achieves several things:

  • Re-ordering existing data to match a set of labels
  • Inserting NaN markers where no data exists for a label
  • Possibly filling missing data for a label using some type of logic (defaulting to adding NaN values)

Re-indexing can be as simple as simply assigning a new index to the .index property of a Series. The following demonstrates changing the index of a Series in this manner:

The number of elements in the list being assigned to the .index property must match the number of rows or an exception will be thrown. Re-indexing also modified the Series in-place.

Flexibility in creating a new index is provided through use of the .reindex...