The structure and content of a DataFrame
can be mutated in several ways. Rows and columns can be added and removed, and data within either can be modified to take on new values. Additionally, columns, as well as index labels, can also be renamed. Each of these will be described in the following sections.
A column can be renamed using the .rename()
method of the DataFrame
. The Book Value
column is inconvenient since it has a space, so we will rename it to BookValue
:
In [48]: # rename the Book Value column to not have a space # this returns a copy with the column renamed df = sp500.rename(columns= {'Book Value': 'BookValue'}) # print first 2 rows df[:2] Out[48]: Sector Price BookValue Symbol MMM Industrials 141.14 26.668 ABT Health Care 39.60 15.573
This has returned a new DataFrame
object with the renamed column...