Getting and Setting Pandas DataFrame Index Names
This tutorial explains how to set and get the name of the index column in a Pandas DataFrame. We will use the following DataFrame example in the article.
import pandas as pd
my_df = pd.DataFrame(
{
"Applicant": ["Ratan", "Anil", "Mukesh", "Kamal"],
"Hometown": ["Delhi", "Pune", "Dhangadi", "Kolkata"],
"Score": [85, 87, 90, 89],
},
index=["2021-01-03", "2021-01-04", "2021-01-05", "2021-01-06"],
)
print(my_df)
Output:
Applicant Hometown Score
2021-01-03 Ratan Delhi 85
2021-01-04 Anil Pune 87
2021-01-05 Mukesh Dhangadi 90
2021-01-06 Kamal Kolkata 89
Get the name of the index column in a DataFrame
We can name
get the name of the index column of a DataFrame through the index column's attribute.
import pandas as pd
my_df = pd.DataFrame(
{
"Applicant": ["Ratan", "Anil", "Mukesh", "Kamal"],
"Hometown": ["Delhi", "Pune", "Dhangadi", "Kolkata"],
"Score": [85, 87, 90, 89],
},
index=["2021-01-03", "2021-01-04", "2021-01-05", "2021-01-06"],
)
print("The DataFrame is:")
print(my_df, "\n")
print("Name of Index Column of the DataFrame is:")
print(my_df.index.name)
Output:
The DataFrame is:
Applicant Hometown Score
2021-01-03 Ratan Delhi 85
2021-01-04 Anil Pune 87
2021-01-05 Mukesh Dhangadi 90
2021-01-06 Kamal Kolkata 89
Name of Index Column of the DataFrame is:
None
Since we did not my_df
set the name of the index column for the DataFrame, my_df
the index column name of the DataFrame is obtained None
.
name
You can set the name of the index column of a DataFrame by setting the
We can set the name of the DataFrame's index column by simply setting the value index
of the DataFrame's index attribute .name
import pandas as pd
my_df = pd.DataFrame(
{
"Applicant": ["Ratan", "Anil", "Mukesh", "Kamal"],
"Hometown": ["Delhi", "Pune", "Dhangadi", "Kolkata"],
"Score": [85, 87, 90, 89],
},
index=["2021-01-03", "2021-01-04", "2021-01-05", "2021-01-06"],
)
print("Initial DataFrame:")
print(my_df, "\n")
my_df.index.name = "Date"
print("DataFrame after setting the name of Index Column:")
print(my_df, "\n")
print("Name of Index Column of the DataFrame is:")
print(my_df.index.name)
Output:
Initial DataFrame:
Applicant Hometown Score
2021-01-03 Ratan Delhi 85
2021-01-04 Anil Pune 87
2021-01-05 Mukesh Dhangadi 90
2021-01-06 Kamal Kolkata 89
DataFrame after setting the name of Index Column:
Applicant Hometown Score
Date
2021-01-03 Ratan Delhi 85
2021-01-04 Anil Pune 87
2021-01-05 Mukesh Dhangadi 90
2021-01-06 Kamal Kolkata 89
Name of Index Column of the DataFrame is:
Date
It sets the value of the property my_df
of .index
Date
Use rename_axis()
the method to set the name of the index column of the DataFrame.
We can set the name of the index column rename_axis()
in the DataFrame by passing the name of the index column as an argument to the method.
import pandas as pd
my_df = pd.DataFrame(
{
"Applicant": ["Ratan", "Anil", "Mukesh", "Kamal"],
"Hometown": ["Delhi", "Pune", "Dhangadi", "Kolkata"],
"Score": [85, 87, 90, 89],
},
index=["2021-01-03", "2021-01-04", "2021-01-05", "2021-01-06"],
)
print("Initial DataFrame:")
print(my_df, "\n")
my_df = my_df.rename_axis("Date")
print("DataFrame after setting the name of Index Column:")
print(my_df, "\n")
print("Name of Index Column of the DataFrame is:")
print(my_df.index.name)
Output:
Initial DataFrame:
Applicant Hometown Score
2021-01-03 Ratan Delhi 85
2021-01-04 Anil Pune 87
2021-01-05 Mukesh Dhangadi 90
2021-01-06 Kamal Kolkata 89
DataFrame after setting the name of Index Column:
Applicant Hometown Score
Date
2021-01-03 Ratan Delhi 85
2021-01-04 Anil Pune 87
2021-01-05 Mukesh Dhangadi 90
2021-01-06 Kamal Kolkata 89
Name of Index Column of the DataFrame is:
Date
It sets the column names of rename_axis()
the DataFrame to using the method .my_df
index
Date
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Finding the installed version of Pandas
Publish Date:2025/04/12 Views:190 Category:Python
-
Pandas is one of the commonly used Python libraries for data analysis, and Pandas versions need to be updated regularly. Therefore, other Pandas requirements are incompatible. Let's look at ways to determine the Pandas version and dependenc
KeyError in Pandas
Publish Date:2025/04/12 Views:81 Category:Python
-
This tutorial explores the concept of KeyError in Pandas. What is Pandas KeyError? While working with Pandas, analysts may encounter multiple errors thrown by the code interpreter. These errors are wide ranging and can help us better invest
Grouping and Sorting in Pandas
Publish Date:2025/04/12 Views:90 Category:Python
-
This tutorial explored the concept of grouping data in a DataFrame and sorting it in Pandas. Grouping and Sorting DataFrame in Pandas As we know, Pandas is an advanced data analysis tool or package extension in Python. Most of the companies
Plotting Line Graph with Data Points in Pandas
Publish Date:2025/04/12 Views:65 Category:Python
-
Pandas is an open source data analysis library in Python. It provides many built-in methods to perform operations on numerical data. Data visualization is very popular nowadays and is used to quickly analyze data visually. We can visualize
Converting Timedelta to Int in Pandas
Publish Date:2025/04/12 Views:123 Category:Python
-
This tutorial will discuss converting a to a using dt the attribute in Pandas . timedelta int Use the Pandas dt attribute to timedelta convert int To timedelta convert to an integer value, we can use the property pandas of the library dt .
Pandas fill NaN values
Publish Date:2025/04/12 Views:93 Category:Python
-
This tutorial explains how we can use DataFrame.fillna() the method to fill NaN values with specified values. We will use the following DataFrame in this article. import numpy as np import pandas as pd roll_no = [ 501 , 502 , 503 , 50
Pandas Convert String to Number
Publish Date:2025/04/12 Views:147 Category:Python
-
This tutorial explains how to pandas.to_numeric() convert string values of a Pandas DataFrame into numeric type using the method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 , 103 , 343 , 565 ], "Name" :
How to Change the Data Type of a Column in Pandas
Publish Date:2025/04/12 Views:139 Category:Python
-
We will look at methods for changing the data type of columns in a Pandas Dataframe, as well as options like to_numaric , , as_type and infer_objects . We will also discuss how to to_numaric use downcasting the option in . to_numeric Method
Get the first row of Dataframe Pandas
Publish Date:2025/04/12 Views:78 Category:Python
-
This tutorial explains how to use the get_first_row pandas.DataFrame.iloc attribute and pandas.DataFrame.head() get_first_row method from a Pandas DataFrame. We will use the following DataFrame in the following example to explain how to get