O'Reilly logo

Learning pandas 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

Resetting and reindexing

A DataFrame can have its index reset by using the .reset_index(). A common use of this, is to move the contents of a DataFrame object's index into one or more columns. The following code moves the symbols in the index of sp500 into a column and replaces the index with a default integer index. The result is a new DataFrame, not an in-place update. The code is as follows:

In [102]:
   # reset the index, moving it into a column
   reset_sp500 = sp500.reset_index()
   reset_sp500

Out[102]:
       Symbol                  Sector   Price  BookValue
   0      MMM             Industrials  141.14     26.668
   1      ABT             Health Care   39.60     15.573
   2     ABBV             Health Care   53.95      2.954
   3      ACN  Information Technology   79.79      8.326
   4      ACE              Financials  102.91     86.897
   ..     ...                     ...     ...        ...
 495 YHOO Information ...

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