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

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 Technology   35.02     12.768
   496    YUM  Consumer Discretionary...