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.

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

If You’re in Hurry…

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

It resets the existing index of the dataframe by making it a new column and 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

If You Want to Understand Details, Read on…

Different methods are available to convert the index of a pandas dataframe to a column. You can also use different column names while converting the index into a column. Let us explore it in detail.

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.

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.

If there are multiple indexes, you can convert either all indexes or only a specific index as a column. This is discussed in the later section of the tutorial.

Code

The following code demonstrates how to use the reset_index() method to convert the index into a column in the same dataframe.

df.reset_index(inplace=True)

df

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.

Dataframe Will Look Like

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.

Next, use the Pandas rename method to use a new column header for the converted index column.

Code

The following code demonstrates how to convert the index into a new column and use a new column header using the rename method.

df.reset_index(inplace=True)

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

df

The index column has the new name S.No.

Dataframe Will Look Like

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.

Code

The following code demonstrates how to rename the column axis.

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

df

The index is renamed as S.No.

Dataframe Will Look Like

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

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

Dataframe Will Look Like

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.

Using this method, you can use a custom name for the column in just one step.

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.

The method converts all the existing indexes into a new column and 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. You can 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

Remove Index instead of Converting it into a column

You may want to remove the index instead of converting it into a column.

To demonstrate this, First, set a custom index to the pandas dataframe. Any existing column can be set as an index using the set_index() method.

Code

The following code demonstrates how to set the Country Code as an index.

df.set_index('Country Code', inplace=True)

df

Dataframe Will Look Like

The country code column is set as an index.

First NameLast NameCountry
Country Code
1ShivamPandeyIndia
2KumarRamUS
3FelixJohnGermany
1MichaelJohnIndia

Code

The following code demonstrates how to drop the index using the reset_index() method.

The drop operation is performed using the additional parameter drop=true.

df.reset_index(drop=True, inplace=True)

df

Datafame Will Look Like

The country code index is removed, and it is not added as a new column as well.

First NameLast NameCountry
0ShivamPandeyIndia
1KumarRamUS
2FelixJohnGermany
3MichaelJohnIndia

Conclusion

You’ve learned how to convert the index of a pandas dataframe to a column in a single index and multi-index dataframes.

Additionally, you’ve learned how to use a new column header while converting the index to a new column.

If you’ve any questions, feel free to comment below.

You May Also Like

Leave a Comment