In this tutorial, you’ll learn how to use Pandas to rename an index, including how to rename a Pandas dataframe index and a Pandas multi-index dataframe. By renaming a Pandas dataframe index, you’re changing the name of the index column.
The Quick Answer: Use df.index.names
Table of Contents
Loading a Sample Dataframe
If you want to follow along with the dataframe, feel free to copy and paste the code below into your code editor of choice. If you have your own dataframe you’re using, you may need to adjust some of the code to follow along.
Let’s get started!
import pandas as pd
df = pd.DataFrame.from_dict(
{
'Year': [2018, 2019, 2020, 2021],
'Carl': [1000, 2300, 1900, 3400],
'Jane': [1500, 1700, 1300, 800],
'Melissa': [800, 2300, None, 2300]
}
).set_index('Year')
print(df)
This returns the following dataframe:
Carl Jane Melissa
Year
2018 1000 1500 800.0
2019 2300 1700 2300.0
2020 1900 1300 NaN
2021 3400 800 2300.0
Let’s get started on renaming a Pandas dataframe index.
How to Rename a Pandas Dataframe Index
Pandas makes it very easy to rename a dataframe index. Before we dive into that, let’s see how we can access a dataframe index’s name.
We can access the dataframe index’s name by using the df.index.name
attribute.
Let’s see what that looks like in Python:
# Get a dataframe index name
index_name = df.index.names
print(index_name)
# Returns: ['Year']
We can see that when we use the .names
attribute, that a list of all the index names are returned. Similarly, we could use the .name
attribute if we know we only have one index. This would return just the value, rather than a list of values.
So, to rename a Pandas dataframe index, we can simply assign something to that attribute. This can be particularly helpful when renaming a Pandas dataframe index after using a Pandas pivot table function.
Want to learn more? Check out my in-depth guide to Pandas pivot tables in my post here.
Let’s see how this looks in Python, by renaming our index to 'Time Period'
:
df.index.names = ['Time Period']
print(df)
This returns the new dataframe below, with a renamed index:
Carl Jane Melissa
Time Period
2018 1000 1500 800.0
2019 2300 1700 2300.0
2020 1900 1300 NaN
2021 3400 800 2300.0
Now, make note of the fact that we passed in a list
of names. If we had used the .name
attribute, we could have simply passed in the string 'Time Period'
.
Now that you know how to rename a Pandas dataframe index, let’s see how to rename a multi-index dataframe.
How to Rename a Pandas Multi-Index
Working with Pandas multi-index dataframes can be a tricky thing – but renaming their indices doesn’t need to be.
This actually works in the same way as the method above. We directly assign a list of values to the .names
attribute.
Let’s load a multi-index dataframe and see how we can rename its index names:
import pandas as pd
df = pd.DataFrame.from_dict(
{
'Year': [2018, 2019, 2020, 2021],
'Carl': [1000, 2300, 1900, 3400],
'Jane': [1500, 1700, 1300, 800],
'Melissa': [800, 2300, None, 2300]
}
).set_index(['Year', 'Carl'])
print(df)
This returns the following Pandas dataframe:
Jane Melissa
Year Carl
2018 1000 1500 800.0
2019 2300 1700 2300.0
2020 1900 1300 NaN
2021 3400 800 2300.0
Now, when we want to see the names of the multi-index dataframe, we can call the .names
attribute again:
print(df.index.names)
# Returns: ['Year', 'Carl']
Similarly, we can rename a multi-index dataframe indices by assigning a list to the .names
attribute. Let’s say we want to name them: ‘Time Period’ and ‘Average Sales’:
df.index.names = ['Time Period', 'Average Sales']
print(df)
This returns the following dataframe:
Jane Melissa
Time Period Average Sales
2018 1000 1500 800.0
2019 2300 1700 2300.0
2020 1900 1300 NaN
2021 3400 800 2300.0
In this section, you learned how to rename the indices of a multi-index dataframe. In the final section, you’ll learn how to remove a Pandas dataframe index entirely.
How to Remove a Pandas Index Name
There may be many times when you don’t want your Pandas dataframe’s index to be called anything. This can be particularly useful after you create a Pandas pivot table, since the index names can often be misleading.
To remove a Pandas dataframe index name, we can simply assign it an empty string ''
or the value of None
. Lets see how we can do this with our original dataframe:
import pandas as pd
df = pd.DataFrame.from_dict(
{
'Year': [2018, 2019, 2020, 2021],
'Carl': [1000, 2300, 1900, 3400],
'Jane': [1500, 1700, 1300, 800],
'Melissa': [800, 2300, None, 2300]
}
).set_index('Year')
df.index.name = None
print(df)
This returns a dataframe without a named index, as shown below:
Carl Jane Melissa
2018 1000 1500 800.0
2019 2300 1700 2300.0
2020 1900 1300 NaN
2021 3400 800 2300.0
In this section, you learned how to remove a Pandas dataframe index name.
Conclusion
In this post, you learned how to rename a Pandas dataframe index, using the .name
and .names
attributes. You learned how to rename the index on a single-index dataframe as well as a multi-index dataframe. Finally, you learned how to remove a Pandas index name.
To learn more about the Pandas dataframe index.name
attribute, check out the official documentation here.
Thanks very helpful and to the point!
Thanks so much Danly!