How To Change the Index of a Dataframe in Python
Changing the index of a dataframe to pandas and its advantages.
--
When we generate a dataframe or import a dataset with pandas, it automatically creates a column that acts as an “index”. In this short article, we will see together how to attribute the index function to another column (or to more columns) and above all what are the advantages of this operation.
The Index column
Let’s take a simple example by importing a CSV file into pandas that contain a list of some people with their data. Our data set looks like this:
By importing the CSV file into pandas (as explained here ), we will have a dataframe like this:
Surprise! An untitled column with numbers has been created to the left of ‘Name’ column. This is the index column of our dataframe.
Changing the column that acts as an index
Of course, there is the possibility to change the column that plays the index role of our dataframe. We just need to use the set_index() command.
df. set_index( 'Name' )
In this way, the “Name” column has become the index of the dataframe.
Setting multiple columns as indices
It is possible to have multiple columns as an index within our dataframe. To do this, we just have to include some keywords inside the set_index() command.
df. set_index([ 'Name' , 'Age' ])
By doing so, our dataframe will look like this: