O'Reilly logo

Learning pandas - Second Edition by Michael Heydt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Reindexing a pandas object

A DataFrame can be reindexed using the .reindex() method. Reindexing makes the DataFrame conform to the new index, aligning data from the old index with the new and filling in NaN where alignment fails.

This code demonstrates re-indexing the sp500 to the three specified index labels.

This process creates a new DataFrame with specified rows. If a row is not found for a specific value, then NaN values are inserted as is seen with the 'FOO' label. This method is actually a good technique of filtering out data based on the index labels.

Reindexing can also be done upon columns, as demonstrated by the following code for ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required