How to convert index of a Pandas Dataframe into a column

  1. df.index to add index as a new column
  2. reset_index with rename axis to rename the current index column name
  3. set_index method to convert column to index
  4. MultiIndex to set multiple layers of indexes on column

We will introduce the various methods to convert the index of a Pandas dataframe into a column, like df.index, reset_index with rename axis to rename the index and set_index.

We will also introduce how we can apply Multi-Index to given Dataframewith multiple layers of indexes.

df.index to add index as a new column

The simplest way to add index as the column is by adding df.index as a new column to dataframe.

Consider the following code:

# python 3.x
import pandas as pd
df = pd.DataFrame([
    (1,2,None),
    (None,4,None),
    (5,None,7),
    (5,None,None)
    ],columns=['a','b','d'])
df['index'] = df.index
print(df)

Output:

     a    b    d  index1
0  1.0  2.0  NaN       0
1  NaN  4.0  NaN       1
2  5.0  NaN  7.0       2
3  5.0  NaN  NaN       3

reset_index with rename axis to rename the current index column name

We can change the name of our index, then use reset_index to a series:

# python 3.x
import pandas as pd

df = pd.DataFrame([
    (1,2,None),
    (None,4,None),
    (5,None,7),
    (5,None,None)], 
    columns=['a','b','d'])
df.df.rename_axis('index').reset_index()
print(df)

Output:

   index    a    b    d
0      0  1.0  2.0  NaN
1      1  NaN  4.0  NaN
2      2  5.0  NaN  7.0
3      3  5.0  NaN  NaN

set_index method to convert column to index

We can convert any column to index using set_index method:

# python 3.x
import pandas as pd
df = pd.DataFrame([
    (1,2,None),
    (None,4,None),
    (5,4,7),
    (5,5,None)], 
    columns=['a','b','d'])
df.set_index('b',inplace=True)
print(df)

Output:

     a    d
b          
2  1.0  NaN
4  NaN  NaN
4  5.0  7.0
5  5.0  NaN

or if we want to remove the index name, as in the original we can do df.index.name = None:

# python 3.x
import pandas as pd
df = pd.DataFrame([
    (1,2,None),
    (None,4,None),
    (5,4,7),
    (5,5,None)
    ],columns=['a','b','d'])
df.set_index('b',inplace=True)
df.index.name = None
print(df)

Output:

     a    d
2  1.0  NaN
4  NaN  NaN
4  5.0  7.0
5  5.0  NaN

MultiIndex to set multiple layers of indexes on column

We can use MultiIndex.from_product() function to make a MultiIndex as follow:

# python 3.x
import pandas as pd
import numpy as np
index = pd.MultiIndex.from_product([
    ['Burger', 'Steak', 'Sandwich'], 
    ['Half', 'Full']], 
    names=['Item', 'Type'])
df = pd.DataFrame(index=index, 
                  data=np.random.randint
                  (0, 10, (6,4)), 
                  columns=list('abcd'))
print(df)

Output:

               a  b  c  d
Item     Type            
Burger   Half  0  3  9  1
         Full  2  2  0  5
Steak    Half  8  4  5  5
         Full  5  8  0  7
Sandwich Half  2  8  9  5
         Full  4  4  5  9

Related Article - Pandas DataFrame

  • How to Get Pandas DataFrame Column Headers as a List
  • How to Delete Pandas DataFrame Column
  • How to Convert DataFrame Column to Datetime in Pandas
  • How to Convert a float to an integer in Pandas DataFrame
  • How to Sort Pandas DataFrame by One Column's Values
  • How to get the aggregate of Pandas group-by and Sum
  • How to convert Python dictionary to Pandas DataFrame
  • How to add header row to a pandas DataFrame
  • How to convert Pandas Dataframe to Numpy array
  • How to count the NaN occurrences in a column in Pandas Dataframe
  • How to change the order of Pandas DataFrame columns
  • How to add one row to Pandas DataFrame
  • How to delete a row based on column value in Pandas DataFrame
  • How to get a value from a cell of a Pandas DataFrame
  • comments powered by Disqus