How To Convert the Index Of a Pandas Dataframe To A Column – With Examples

Pandas dataframe contains an index that is used to identify rows uniquely.

You can convert the index of a pandas dataframe to a column using the df.reset_index(inplace=True) statement.

Basic Example

  • The reset index method resets the existing index of the dataframe by making it a new column
  • Uses the default index 0,1,...n as the index.
df.reset_index(inplace=True)

df

Dataframe Will Look Like

The index column is added as a new column of the dataframe, and the default index 0,1,..n is assigned as the index.

indexFirst NameLast NameCountryCountry Code
00ShivamPandeyIndia1
11KumarRamUS2
22FelixJohnGermany3
33MichaelJohnIndia1

This tutorial explains to you how to convert the index of a pandas dataframe to a column using different methods.

Creating Dataframe

Use the following code to create a Pandas dataframe.

It contains four columns with data.

No index is assigned explicitly. Hence, pandas will use the default index.

import pandas as pd 


users = [ ('Shivam', 'Pandey', 'India', 1),
             ('Kumar', 'Ram' , 'US', 2 ),
         ('Felix','John' , 'Germany', 3 ),
         ('Michael','John' , 'India', 1 ),
              ]


df = pd.DataFrame(  users, 
                    columns = ['First Name' , 'Last Name', 'Country', 'Country Code']
                    ) 

df

Dataframe Will Look Like

First NameLast NameCountryCountry Code
0ShivamPandeyIndia1
1KumarRamUS2
2FelixJohnGermany3
3MichaelJohnIndia1

Using Reset_index

You can use the reset_index method to convert the index of the pandas dataframe to a column.

The reset index method,

  • Converts the existing index to a new column of the dataframe. Uses the name index for the new column
  • After converting it, the default index 0,1,2..n is used as the index
  • If there are no explicit index, the default index is converted into a column, and again, a default index will be created for the dataframe

inplace=True parameter is used to denote that the reset operation must be performed in the same dataframe instead of creating a new copy as a result.

Code

df.reset_index(inplace=True)

df

Dataframe Will Look Like

The sample dataframe doesn’t contain any explicit index. It has only the default one. Hence, it is converted as a column, and the default index is again used in the dataframe.

indexFirst NameLast NameCountryCountry Code
00ShivamPandeyIndia1
11KumarRamUS2
22FelixJohnGermany3
33MichaelJohnIndia1

Using Reset_index and New Column Header

This section teaches you how to convert the index of a pandas dataframe to a new column and use a new column header for the new column.

You can

  • Convert the index to a new column using the reset_index() method.
  • Use the Pandas rename method to use a new column header for the converted index column.

Code

df.reset_index(inplace=True)

df = df.rename(columns = {'index':'S.No.'})

df

Dataframe Will Look Like

The index column has the new name S.No.

S.No.First NameLast NameCountryCountry Code
00ShivamPandeyIndia1
11KumarRamUS2
22FelixJohnGermany3
33MichaelJohnIndia1

This is how the rename method is used to use a new column header.

Using reset_axis and Reset_index

You can also use the rename_axis() method to rename the index column while converting it into a column.

  • It first sets the name for the axis.
  • By default, it uses the column axis. Hence, you need not use the axis parameter explicitly.

Code

The following code demonstrates how to rename the column axis.

df.rename_axis('S.No.',inplace=True)

df

Dataframe Will Look Like

The index is renamed as S.No.

First NameLast NameCountryCountry Code
S.No.
0ShivamPandeyIndia1
1KumarRamUS2
2FelixJohnGermany3
3MichaelJohnIndia1

Code

You can use the reset_index() method to convert the index of a pandas dataframe to a column.

df.reset_index(inplace=True)

df

Dataframe Will Look Like

The index column S.No. is converted to a column and added to the pandas dataframe.

S.No.First NameLast NameCountryCountry Code
00ShivamPandeyIndia1
11KumarRamUS2
22FelixJohnGermany3
33MichaelJohnIndia1

Using Simple Assignment

You can use the assignment operator = to assign the index values as a new column to the dataframe.

Code

df['index'] = df.index

df

Dataframe Will Look Like

First NameLast NameCountryCountry Codeindex
0ShivamPandeyIndia10
1KumarRamUS21
2FelixJohnGermany32
3MichaelJohnIndia13

Creating Multiple Index Dataframe

Pandas dataframe can contain multiple and multi-level indexes.

Create a dataframe with multiple indexes, namely ’Code’, ‘ID’.

Code

index_names = pd.MultiIndex.from_tuples([('A', '1'),
                                       ('B', '2'),
                                       ('C', '3'),
                                       ('D', '4')],
                                       names=['Code', 'ID'])

users = [ ('Shivam', 'Pandey', 'India', 1),
             ('Kumar', 'Ram' , 'US', 2 ),
         ('Felix','John' , 'Germany', 3 ),
         ('Michael','John' , 'India', 1 ),
              ]

df = pd.DataFrame(users,  columns = ['First Name' , 'Last Name', 'Country', 'Country Code'], index=index_names)

df

Dataframe Will Look Like

The sample dataframe contains two column indexes.

First NameLast NameCountryCountry Code
CodeID
A1ShivamPandeyIndia1
B2KumarRamUS2
C3FelixJohnGermany3
D4MichaelJohnIndia1

Now you’ll use this dataframe to learn how to convert the multiple indexes to a column name.

Convert Multiindex to Multiple Columns

You can use the reset_index() method to convert the multiple indexes to multiple columns.

  • Converts all the existing indexes into a new column
  • Uses the default index as an index.

Code

df.reset_index(inplace=True)

df

Dataframe Will Look Like

CodeIDFirst NameLast NameCountryCountry Code
0A1ShivamPandeyIndia1
1B2KumarRamUS2
2C3FelixJohnGermany3
3D4MichaelJohnIndia1

Convert Specific Indexes of Multiple Indexes into a column

You can use the reset_index() method to convert the specific indexes into a new column.

To convert a specific index,

  • Use the level parameter.
  • Pass the multiple columns as a list to this parameter.

Code

The following code demonstrates how to use the reset_index() method with the level = ['ID'] parameter to convert the index ID into a new column.

df.reset_index(inplace=True, level = ['ID'])

df

Dataframe Will Look Like

The id index is converted into a new column of the dataframe.

IDFirst NameLast NameCountryCountry Code
Code
A1ShivamPandeyIndia1
B2KumarRamUS2
C3FelixJohnGermany3
D4MichaelJohnIndia1

Additional Resources

Leave a Comment