Rename Pandas DataFrame Index
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Quirky Dreamscape Looping
--
Chapters
00:00 Rename Pandas Dataframe Index
00:35 Accepted Answer Score 429
01:32 Answer 2 Score 21
02:05 Answer 3 Score 48
02:21 Answer 4 Score 125
04:47 Thank you
--
Full question
https://stackoverflow.com/questions/1985...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas #dataframe #rename
#avk47
ACCEPTED ANSWER
Score 429
The rename method takes a dictionary for the index which applies to index values.
You want to rename to index level's name:
df.index.names = ['Date']
A good way to think about this is that columns and index are the same type of object (Index or MultiIndex), and you can interchange the two via transpose.
This is a little bit confusing since the index names have a similar meaning to columns, so here are some more examples:
In [1]: df = pd.DataFrame([[1, 2, 3], [4, 5 ,6]], columns=list('ABC'))
In [2]: df
Out[2]:
A B C
0 1 2 3
1 4 5 6
In [3]: df1 = df.set_index('A')
In [4]: df1
Out[4]:
B C
A
1 2 3
4 5 6
You can see the rename on the index, which can change the value 1:
In [5]: df1.rename(index={1: 'a'})
Out[5]:
B C
A
a 2 3
4 5 6
In [6]: df1.rename(columns={'B': 'BB'})
Out[6]:
BB C
A
1 2 3
4 5 6
Whilst renaming the level names:
In [7]: df1.index.names = ['index']
df1.columns.names = ['column']
Note: this attribute is just a list, and you could do the renaming as a list comprehension/map.
In [8]: df1
Out[8]:
column B C
index
1 2 3
4 5 6
ANSWER 2
Score 125
The currently selected answer does not mention the rename_axis method which can be used to rename the index and column levels.
Pandas has some quirkiness when it comes to renaming the levels of the index. There is also a new DataFrame method rename_axis available to change the index level names.
Let's take a look at a DataFrame
df = pd.DataFrame({'age':[30, 2, 12],
'color':['blue', 'green', 'red'],
'food':['Steak', 'Lamb', 'Mango'],
'height':[165, 70, 120],
'score':[4.6, 8.3, 9.0],
'state':['NY', 'TX', 'FL']},
index = ['Jane', 'Nick', 'Aaron'])
This DataFrame has one level for each of the row and column indexes. Both the row and column index have no name. Let's change the row index level name to 'names'.
df.rename_axis('names')
The rename_axis method also has the ability to change the column level names by changing the axis parameter:
df.rename_axis('names').rename_axis('attributes', axis='columns')
If you set the index with some of the columns, then the column name will become the new index level name. Let's append to index levels to our original DataFrame:
df1 = df.set_index(['state', 'color'], append=True)
df1
Notice how the original index has no name. We can still use rename_axis but need to pass it a list the same length as the number of index levels.
df1.rename_axis(['names', None, 'Colors'])
You can use None to effectively delete the index level names.
Series work similarly but with some differences
Let's create a Series with three index levels
s = df.set_index(['state', 'color'], append=True)['food']
s
state color
Jane NY blue Steak
Nick TX green Lamb
Aaron FL red Mango
Name: food, dtype: object
We can use rename_axis similarly to how we did with DataFrames
s.rename_axis(['Names','States','Colors'])
Names States Colors
Jane NY blue Steak
Nick TX green Lamb
Aaron FL red Mango
Name: food, dtype: object
Notice that the there is an extra piece of metadata below the Series called Name. When creating a Series from a DataFrame, this attribute is set to the column name.
We can pass a string name to the rename method to change it
s.rename('FOOOOOD')
state color
Jane NY blue Steak
Nick TX green Lamb
Aaron FL red Mango
Name: FOOOOOD, dtype: object
DataFrames do not have this attribute and infact will raise an exception if used like this
df.rename('my dataframe')
TypeError: 'str' object is not callable
Prior to pandas 0.21, you could have used rename_axis to rename the values in the index and columns. It has been deprecated so don't do this
ANSWER 3
Score 48
For newer pandas versions
df.index = df.index.rename('new name')
or
df.index.rename('new name', inplace=True)
The latter is required if a data frame should retain all its properties.
ANSWER 4
Score 21
In Pandas version 0.13 and greater the index level names are immutable (type FrozenList) and can no longer be set directly. You must first use Index.rename() to apply the new index level names to the Index and then use DataFrame.reindex() to apply the new index to the DataFrame. Examples:
For Pandas version < 0.13
df.index.names = ['Date']
For Pandas version >= 0.13
df = df.reindex(df.index.rename(['Date']))




