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 ...
Get Learning pandas now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.