In this article, we will discuss how to convert indexes of a dataframe or a multi-index dataframe into its columns.
Pandas Dataframe class provides a function to reset the indexes of the dataframe i.e.
Dataframe.reset_index()
DataFrame.reset_index(self, level=None, drop=False, inplace=False, col_level=0, col_fill='')
It resets the indexes of the dataframe and returns a dataframe with a new index.
Important arguments are as follows,
- level : int, str or list of str
- By default, reset_index() resets all the indexes of the dataframe. In case of a multi-index dataframe, if we want to reset some specific indexes, then we can specify it as int, str, or list of str, i.e., index names.
- drop: bool, by default False
- If False, then converts the index to a column else removes the index from the dataframe.
- inplace: bool, default False
- If True, then modifies the dataframe in place.
Returns:
- If inplace argument is True then returns None and modifies the existing dataframe. Whereas, if inplace is False, then returns a copy of existing dataframe object with modifications, i.e. doesn’t alter calling dataframe object.
We will use this function to convert the indexes of a dataframe to columns.
First of all, create a dataframe and set its index, i.e.
# List of Tuples empoyees = [(11, 'jack', 34, 'Sydney', 70000) , (12, 'Riti', 31, 'Delhi' , 77000) , (13, 'Aadi', 16, 'Mumbai', 81000) , (14, 'Mohit', 31,'Delhi' , 90000) , (15, 'Veena', 12, 'Delhi' , 91000) , (16, 'Shaunak', 35, 'Mumbai', 75000 ), (17, 'Shaun', 35, 'Colombo', 63000) ] # Create a DataFrame object empDfObj = pd.DataFrame(empoyees, columns=['ID' , 'Name', 'Age', 'City', 'Salary']) # Set 'ID' as the index of the dataframe empDfObj.set_index('ID', inplace=True) print(empDfObj)
Contents of the created dataframe object empDfObj are,
Frequently Asked:
Name Age City Salary ID 11 jack 34 Sydney 70000 12 Riti 31 Delhi 77000 13 Aadi 16 Mumbai 81000 14 Mohit 31 Delhi 90000 15 Veena 12 Delhi 91000 16 Shaunak 35 Mumbai 75000 17 Shaun 35 Colombo 63000
Now let’s experiment with this dataframe,
Convert index of a Dataframe into a column of dataframe
To convert the index ‘ID‘ of the dataframe empDfObj into a column, call the reset_index() function on that dataframe,
# Reset the index of dataframe modified = empDfObj.reset_index() print("Modified Dataframe : ") print(modified)
Output:
ID Name Age City Salary 0 11 jack 34 Sydney 70000 1 12 Riti 31 Delhi 77000 2 13 Aadi 16 Mumbai 81000 3 14 Mohit 31 Delhi 90000 4 15 Veena 12 Delhi 91000 5 16 Shaunak 35 Mumbai 75000 6 17 Shaun 35 Colombo 63000
As we didn’t provided inplace argument, so by default it returned a modified copy of the dataframe. In which index ‘ID’ gets converted into the column ‘ID’ in the dataframe, and a new default index gets assigned to it. Now, if you want to make changes in the existing dataframe then either assign it back to empDFObj like,
empDfObj = empDfObj.reset_index()
or pass implace argument as True, i.e.
empDfObj.reset_index(inplace=True) print(empDfObj)
Output:
ID Name Age City Salary 0 11 jack 34 Sydney 70000 1 12 Riti 31 Delhi 77000 2 13 Aadi 16 Mumbai 81000 3 14 Mohit 31 Delhi 90000 4 15 Veena 12 Delhi 91000 5 16 Shaunak 35 Mumbai 75000 6 17 Shaun 35 Colombo 63000
It modified the dataframe object empDfObj in place.
Let’s set the column ‘ID’ as index of the dataframe for more examples,
# Set 'ID' as the index of the dataframe empDfObj.set_index('ID', inplace=True)
Contents of the Dataframe object empDfObj are now,
Name Age City Salary ID 11 jack 34 Sydney 70000 12 Riti 31 Delhi 77000 13 Aadi 16 Mumbai 81000 14 Mohit 31 Delhi 90000 15 Veena 12 Delhi 91000 16 Shaunak 35 Mumbai 75000 17 Shaun 35 Colombo 63000
Remove index of dataframe instead of converting into column
In previous example, we converted the dataframe index in to the column but what if we just want to remove the index of the dataframe instead of moving it as column. We can do that by passing drop argument as True in the reset_index() function,
# Remove index ID instead of converting into a column modified = empDfObj.reset_index(drop=True) print("Modified Dataframe : ") print(modified)
Output
Name Age City Salary 0 jack 34 Sydney 70000 1 Riti 31 Delhi 77000 2 Aadi 16 Mumbai 81000 3 Mohit 31 Delhi 90000 4 Veena 12 Delhi 91000 5 Shaunak 35 Mumbai 75000 6 Shaun 35 Colombo 63000
It removed the index of the dataframe and assigned a default index to the dataframe.
Resetting indexes of a Multi-Index Dataframe
Let’s convert the dataframe object empDfObj into a multi-index dataframe with two indexes i.e. ID & Name,
# Create a DataFrame object empDfObj = pd.DataFrame(empoyees, columns=['ID', 'Name', 'Age', 'City', 'Salary']) # set multiple columns as the index of the the dataframe to # make it multi-index dataframe. empDfObj.set_index(['ID', 'Name'], inplace=True) print(empDfObj)
Contents of the multi-index dataframe empDfObj are,
Age City Salary ID Name 11 jack 34 Sydney 70000 12 Riti 31 Delhi 77000 13 Aadi 16 Mumbai 81000 14 Mohit 31 Delhi 90000 15 Veena 12 Delhi 91000 16 Shaunak 35 Mumbai 75000 17 Shaun 35 Colombo 63000
Convert all the indexes of Multi-index Dataframe to the columns of Dataframe
To convert all the indexes of a multi-index dataframe to columns with same, just call the reset_index() on the dataframe object i.e.
# Reset all indexes of a multi-index dataframe modified = empDfObj.reset_index() print(modified)
Output:
ID Name Age City Salary 0 11 jack 34 Sydney 70000 1 12 Riti 31 Delhi 77000 2 13 Aadi 16 Mumbai 81000 3 14 Mohit 31 Delhi 90000 4 15 Veena 12 Delhi 91000 5 16 Shaunak 35 Mumbai 75000 6 17 Shaun 35 Colombo 63000
It converted the indexes ‘ID’ & ‘Name’ to the columns with same name in the dataframe.
What if we want to convert the only one index of the multi-index dataframe in to the column. We can do that by passing the index name in the level argument i.e.
modified = empDfObj.reset_index(level='ID') print("Modified Dataframe: ") print(modified)
Output
Modified Dataframe: ID Age City Salary Name jack 11 34 Sydney 70000 Riti 12 31 Delhi 77000 Aadi 13 16 Mumbai 81000 Mohit 14 31 Delhi 90000 Veena 15 12 Delhi 91000 Shaunak 16 35 Mumbai 75000 Shaun 17 35 Colombo 63000
It converted the index ‘ID’ into the column ‘ID’ in the dataframe. Whereas, index ‘Name’ remains as it is.
Let’s look at an another example,
modified = empDfObj.reset_index(level='Name') print("Modified Dataframe: ") print(modified)
Output
Modified Dataframe: Name Age City Salary ID 11 jack 34 Sydney 70000 12 Riti 31 Delhi 77000 13 Aadi 16 Mumbai 81000 14 Mohit 31 Delhi 90000 15 Veena 12 Delhi 91000 16 Shaunak 35 Mumbai 75000 17 Shaun 35 Colombo 63000
It converted the index ‘Name’ into the column ‘Name’ in the dataframe. Whereas, index ‘ID’ remains as it is.
We can pass the multiple column names in the level argument as a list i.e.
modified = empDfObj.reset_index(level=['ID', 'Name']) print("Modified Dataframe: ") print(modified)
Output:
Modified Dataframe: ID Name Age City Salary 0 11 jack 34 Sydney 70000 1 12 Riti 31 Delhi 77000 2 13 Aadi 16 Mumbai 81000 3 14 Mohit 31 Delhi 90000 4 15 Veena 12 Delhi 91000 5 16 Shaunak 35 Mumbai 75000 6 17 Shaun 35 Colombo 63000
It converted both the indexes ‘ID’ & ‘Name’ into the columns of the dataframe.
The complete example is as follows,
import pandas as pd def main(): # List of Tuples empoyees = [(11, 'jack', 34, 'Sydney', 70000) , (12, 'Riti', 31, 'Delhi' , 77000) , (13, 'Aadi', 16, 'Mumbai', 81000) , (14, 'Mohit', 31,'Delhi' , 90000) , (15, 'Veena', 12, 'Delhi' , 91000) , (16, 'Shaunak', 35, 'Mumbai', 75000 ), (17, 'Shaun', 35, 'Colombo', 63000) ] # Create a DataFrame object empDfObj = pd.DataFrame(empoyees, columns=['ID' , 'Name', 'Age', 'City', 'Salary']) # Set 'ID' as the index of the dataframe empDfObj.set_index('ID', inplace=True) print("Contents of the Dataframe : ") print(empDfObj) print('Convert the index of Dataframe to the column') # Reset the index of dataframe modified = empDfObj.reset_index() print("Modified Dataframe : ") print(modified) print('Convert the index of Dataframe to the column - in place ') empDfObj.reset_index(inplace=True) print("Contents of the Dataframe : ") print(empDfObj) # Set 'ID' as the index of the dataframe empDfObj.set_index('ID', inplace=True) print('Remove the index of Dataframe to the column') # Remove index ID instead of converting into a column modified = empDfObj.reset_index(drop=True) print("Modified Dataframe : ") print(modified) print('Reseting indexes of a Multi-Index Dataframe') # Create a DataFrame object empDfObj = pd.DataFrame(empoyees, columns=['ID', 'Name', 'Age', 'City', 'Salary']) # set multiple columns as the index of the the dataframe to # make it multi-index dataframe. empDfObj.set_index(['ID', 'Name'], inplace=True) print("Contents of the Multi-Index Dataframe : ") print(empDfObj) print('Convert all the indexes of Multi-index Dataframe to the columns of Dataframe') # Reset all indexes of a multi-index dataframe modified = empDfObj.reset_index() print("Modified Mult-Index Dataframe : ") print(modified) print("Contents of the original Multi-Index Dataframe : ") print(empDfObj) modified = empDfObj.reset_index(level='ID') print("Modified Dataframe: ") print(modified) modified = empDfObj.reset_index(level='Name') print("Modified Dataframe: ") print(modified) modified = empDfObj.reset_index(level=['ID', 'Name']) print("Modified Dataframe: ") print(modified) if __name__ == '__main__': main()
Output:
Contents of the Dataframe : Name Age City Salary ID 11 jack 34 Sydney 70000 12 Riti 31 Delhi 77000 13 Aadi 16 Mumbai 81000 14 Mohit 31 Delhi 90000 15 Veena 12 Delhi 91000 16 Shaunak 35 Mumbai 75000 17 Shaun 35 Colombo 63000 Convert the index of Dataframe to the column Modified Dataframe : ID Name Age City Salary 0 11 jack 34 Sydney 70000 1 12 Riti 31 Delhi 77000 2 13 Aadi 16 Mumbai 81000 3 14 Mohit 31 Delhi 90000 4 15 Veena 12 Delhi 91000 5 16 Shaunak 35 Mumbai 75000 6 17 Shaun 35 Colombo 63000 Convert the index of Dataframe to the column - in place Contents of the Dataframe : ID Name Age City Salary 0 11 jack 34 Sydney 70000 1 12 Riti 31 Delhi 77000 2 13 Aadi 16 Mumbai 81000 3 14 Mohit 31 Delhi 90000 4 15 Veena 12 Delhi 91000 5 16 Shaunak 35 Mumbai 75000 6 17 Shaun 35 Colombo 63000 Remove the index of Dataframe to the column Modified Dataframe : Name Age City Salary 0 jack 34 Sydney 70000 1 Riti 31 Delhi 77000 2 Aadi 16 Mumbai 81000 3 Mohit 31 Delhi 90000 4 Veena 12 Delhi 91000 5 Shaunak 35 Mumbai 75000 6 Shaun 35 Colombo 63000 Reseting indexes of a Multi-Index Dataframe Contents of the Multi-Index Dataframe : Age City Salary ID Name 11 jack 34 Sydney 70000 12 Riti 31 Delhi 77000 13 Aadi 16 Mumbai 81000 14 Mohit 31 Delhi 90000 15 Veena 12 Delhi 91000 16 Shaunak 35 Mumbai 75000 17 Shaun 35 Colombo 63000 Convert all the indexes of Multi-index Dataframe to the columns of Dataframe Modified Mult-Index Dataframe : ID Name Age City Salary 0 11 jack 34 Sydney 70000 1 12 Riti 31 Delhi 77000 2 13 Aadi 16 Mumbai 81000 3 14 Mohit 31 Delhi 90000 4 15 Veena 12 Delhi 91000 5 16 Shaunak 35 Mumbai 75000 6 17 Shaun 35 Colombo 63000 Contents of the original Multi-Index Dataframe : Age City Salary ID Name 11 jack 34 Sydney 70000 12 Riti 31 Delhi 77000 13 Aadi 16 Mumbai 81000 14 Mohit 31 Delhi 90000 15 Veena 12 Delhi 91000 16 Shaunak 35 Mumbai 75000 17 Shaun 35 Colombo 63000 Modified Dataframe: ID Age City Salary Name jack 11 34 Sydney 70000 Riti 12 31 Delhi 77000 Aadi 13 16 Mumbai 81000 Mohit 14 31 Delhi 90000 Veena 15 12 Delhi 91000 Shaunak 16 35 Mumbai 75000 Shaun 17 35 Colombo 63000 Modified Dataframe: Name Age City Salary ID 11 jack 34 Sydney 70000 12 Riti 31 Delhi 77000 13 Aadi 16 Mumbai 81000 14 Mohit 31 Delhi 90000 15 Veena 12 Delhi 91000 16 Shaunak 35 Mumbai 75000 17 Shaun 35 Colombo 63000 Modified Dataframe: ID Name Age City Salary 0 11 jack 34 Sydney 70000 1 12 Riti 31 Delhi 77000 2 13 Aadi 16 Mumbai 81000 3 14 Mohit 31 Delhi 90000 4 15 Veena 12 Delhi 91000 5 16 Shaunak 35 Mumbai 75000 6 17 Shaun 35 Colombo 63000