299

I've a csv file without header, with a DateTime index. I want to rename the index and column name, but with df.rename() only the column name is renamed. Bug? I'm on version 0.12.0

In [2]: df = pd.read_csv(r'D:\Data\DataTimeSeries_csv//seriesSM.csv', header=None, parse_dates=[[0]], index_col=[0] )

In [3]: df.head()
Out[3]: 
                   1
0                   
2002-06-18  0.112000
2002-06-22  0.190333
2002-06-26  0.134000
2002-06-30  0.093000
2002-07-04  0.098667

In [4]: df.rename(index={0:'Date'}, columns={1:'SM'}, inplace=True)

In [5]: df.head()
Out[5]: 
                  SM
0                   
2002-06-18  0.112000
2002-06-22  0.190333
2002-06-26  0.134000
2002-06-30  0.093000
2002-07-04  0.098667
3
  • 58
    i swear in 2022 I still look up this question like 4 times a month.
    – Tommy
    Commented Jan 4, 2022 at 13:20
  • 1
    Don't worry man! I am at the end of 2022 and I do the same -_-.
    – Minions
    Commented Oct 20, 2022 at 2:55
  • 1
    Related.
    – mcp
    Commented Nov 9, 2022 at 18:10

9 Answers 9

432

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
5
  • 16
    Great answer. Just a gentle reminder that without "inplace =True", df1.rename would not really change anything.
    – Sarah
    Commented Sep 29, 2019 at 15:35
  • 1
    @Sarah why did that magical line you mentioned make the change?
    – NAND
    Commented Apr 7, 2021 at 20:07
  • 1
    Inplace modifies the already existing pandas data frame object. While the operation without inplace leaves the data frame untouched and returns a newly created df. Therefore without rename one must do something like this: df1 = df1.rename....
    – Exitare
    Commented Sep 28, 2021 at 16:50
  • 1
    Why is this answer at the bottom? The ones above it don't work. This answer clearly has the most votes. Commented Nov 2, 2021 at 18:09
  • If it's not a multiindex, you can use df.index.name = "name", without surrounding the entry in a list.
    – mcp
    Commented Nov 9, 2022 at 18:11
126

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'])

enter image description here

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')

enter image description here

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')

enter image description here

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

enter image description here

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'])

enter image description here

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

4
  • 1
    Should you swap df1 = df.set_index(['state', 'color'], append=True) with df1.rename_axis(['names', None, 'Colors'])?
    – salhin
    Commented Nov 23, 2017 at 9:46
  • What if I want to rename "Nick" to "Nicolas"? That was what I was looking for when I googled "rename pandas index" and ended up here. EDIT: Oh wait, the accepted answer does explain that, it just wasn't obvious to me at first.
    – Ben Farmer
    Commented Jul 19, 2018 at 9:29
  • 1
    Nice, this is the only answer that can be used in chained assignments!
    – IanS
    Commented Jul 10, 2019 at 9:18
  • 2
    There's no need to call it twice when renaming both the index and columns, you can take care of it once with the args: df.rename_axis(index='names', columns='attributes')
    – ALollz
    Commented Jan 24, 2021 at 19:31
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.

0
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']))
3
  • 9
    Not true! In my version of Pandas (0.13.1) df.index.names = ['foo'] works fine!
    – LondonRob
    Commented Jul 31, 2014 at 15:50
  • 6
    Thanks for noticing that @LondonRob - ` df.index.names = ['foo']` also works for me with Pandas 0.14. Apparently that was only broken briefly and included when I tested it. Commented Aug 3, 2014 at 12:24
  • 1
    Setting names for either index or column directly is changing both for me (on Pandas 0.19), but not with this method.
    – FooBar
    Commented Feb 24, 2017 at 16:36
12

For Single Index :

 df.index.rename('new_name')

For Multi Index :

 df.index.rename(['new_name','new_name2'])

WE can also use this in latest pandas :

rename_axis

3
  • 4
    If you do that you will just have a renamed index as a return but the data frame will not be changed.
    – Jean Paul
    Commented Oct 22, 2020 at 18:40
  • 4
    df.index.rename('new_name', inplace=True) modifies the dataframe in place.
    – oustella
    Commented Aug 4, 2022 at 14:14
  • 1
    FWIW, you can also use a mapper with rename and set_names. df.index.rename({'old_name': 'new_name').
    – sfdurbano
    Commented Nov 4, 2022 at 19:53
9

You can also use Index.set_names as follows:

In [25]: x = pd.DataFrame({'year':[1,1,1,1,2,2,2,2],
   ....:                   'country':['A','A','B','B','A','A','B','B'],
   ....:                   'prod':[1,2,1,2,1,2,1,2],
   ....:                   'val':[10,20,15,25,20,30,25,35]})

In [26]: x = x.set_index(['year','country','prod']).squeeze()

In [27]: x
Out[27]: 
year  country  prod
1     A        1       10
               2       20
      B        1       15
               2       25
2     A        1       20
               2       30
      B        1       25
               2       35
Name: val, dtype: int64
In [28]: x.index = x.index.set_names('foo', level=1)

In [29]: x
Out[29]: 
year  foo  prod
1     A    1       10
           2       20
      B    1       15
           2       25
2     A    1       20
           2       30
      B    1       25
           2       35
Name: val, dtype: int64
0
4

you can use index and columns attributes of pandas.DataFrame. NOTE: number of elements of list must match the number of rows/columns.

#       A   B   C
# ONE   11  12  13
# TWO   21  22  23
# THREE 31  32  33

df.index = [1, 2, 3]
df.columns = ['a', 'b', 'c']
print(df)

#     a   b   c
# 1  11  12  13
# 2  21  22  23
# 3  31  32  33
2

If you want to use the same mapping for renaming both columns and index you can do:

mapping = {0:'Date', 1:'SM'}
df.index.names = list(map(lambda name: mapping.get(name, name), df.index.names))
df.rename(columns=mapping, inplace=True)
1
df.index.rename('new name', inplace=True)

Is the only one that does the job for me (pandas 0.22.0).
Without the inplace=True, the name of the index is not set in my case.

Not the answer you're looking for? Browse other questions tagged or ask your own question.