A DataFrame is the primary data structure of the Pandas library and is commonly used for storing and working with tabular data. A common operation that could be performed on such data is to set the index column title or name in order to add more information to it.
To start working with Pandas, we first need to import it in the Python code :
import pandas as pd
Let us understand this operation with the help of an example. Consider the following DataFrame containing 3 students with names A, B, and C and their corresponding marks (out of 10) for two subjects, Mathematics and Physics.
Code snippet for generating the above DataFrame :
import pandas as pd
# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}
# DataFrame for the dictionary
df = pd.DataFrame(data)
# Printing the DataFrame
print(df)
Here, data is a dictionary we created to initialize the DataFrame. For this, we use the DataFrame() function of the Pandas library which takes the dictionary as an argument and returns the required DataFrame.
Now, let’s say we need to set the title or name of the index column as roll number. The resulting DataFrame would look like this :
Let us look at different ways of performing this operation on a given DataFrame :
This method is pretty straightforward and is the most commonly used one. In this method we use the DataFrame.index.name property and assign the desired value to it, roll number here.
The changes are made in place and there is no need to reassign the DataFrame.
Let us look at the Python code and corresponding output for this method.
import pandas as pd
# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}
# DataFrame for the dictionary
df = pd.DataFrame(data)
# Setting the new index column title as 'index'
df.index.name = 'roll number'
# Printing the new DataFrame
print(df)
Output :
In this method, we use the DataFrame.rename_axis() function to set the index column title or name in an existing DataFrame. We pass the required title, roll number here, as a parameter.
This function returns the updated DataFrame. By default, the changes made are not in place and reassignment of the DataFrame is required.
Let us look at the Python 3 code and corresponding output for this method.
import pandas as pd
# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}
# DataFrame for the dictionary
df = pd.DataFrame(data)
# Setting the new index column title as 'index'
df = df.rename_axis('roll number')
# Printing the new DataFrame
print(df)
Output :
NOTE: The above operation can also be performed in an in place manner, the parameter inplace has to be passed as True in this case. Let us look at the Python 3 code and corresponding output for this method:
import pandas as pd
# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}
# DataFrame for the dictionary
df = pd.DataFrame(data)
# Setting the new index column title as 'index'
df.rename_axis('roll number', inplace=True)
# Printing the new DataFrame
print(df)
Output :
In this method, we use the DataFrame.index.rename() function and pass the first parameter as the desired title, roll number here.
When the second parameter, inplace is set to True, the changes are made in place and there is no need to reassign the DataFrame.
Let us look at the Python 3 code and corresponding output for this method.
import pandas as pd
# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}
# DataFrame for the dictionary
df = pd.DataFrame(data)
# Setting the new index column title as 'index'
df.index.rename('roll number', inplace = True)
# Printing the new DataFrame
print(df)
Output :
In this topic, we have learned to set the index column title or name of an existing DataFrame, following a running example of test scores of students in different subjects, thus giving us an intuition of how this concept could be applied in the real-world situations. Feel free to reach out to info.javaexercise@gmail.com in case of any suggestions.