Pandas index column title or name Pandas index column title or name python python

Pandas index column title or name


You can just get/set the index via its name property

In [7]: df.index.nameOut[7]: 'Index Title'In [8]: df.index.name = 'foo'In [9]: df.index.nameOut[9]: 'foo'In [10]: dfOut[10]:          Column 1foo              Apples          1Oranges         2Puppies         3Ducks           4


You can use rename_axis, for removing set to None:

d = {'Index Title': ['Apples', 'Oranges', 'Puppies', 'Ducks'],'Column 1': [1.0, 2.0, 3.0, 4.0]}df = pd.DataFrame(d).set_index('Index Title')print (df)             Column 1Index Title          Apples            1.0Oranges           2.0Puppies           3.0Ducks             4.0print (df.index.name)Index Titleprint (df.columns.name)None

The new functionality works well in method chains.

df = df.rename_axis('foo')print (df)         Column 1foo              Apples        1.0Oranges       2.0Puppies       3.0Ducks         4.0

You can also rename column names with parameter axis:

d = {'Index Title': ['Apples', 'Oranges', 'Puppies', 'Ducks'],'Column 1': [1.0, 2.0, 3.0, 4.0]}df = pd.DataFrame(d).set_index('Index Title').rename_axis('Col Name', axis=1)print (df)Col Name     Column 1Index Title          Apples            1.0Oranges           2.0Puppies           3.0Ducks             4.0print (df.index.name)Index Titleprint (df.columns.name)Col Name
print df.rename_axis('foo').rename_axis("bar", axis="columns")bar      Column 1foo              Apples        1.0Oranges       2.0Puppies       3.0Ducks         4.0print df.rename_axis('foo').rename_axis("bar", axis=1)bar      Column 1foo              Apples        1.0Oranges       2.0Puppies       3.0Ducks         4.0

From version pandas 0.24.0+ is possible use parameter index and columns:

df = df.rename_axis(index='foo', columns="bar")print (df)bar      Column 1foo              Apples        1.0Oranges       2.0Puppies       3.0Ducks         4.0

Removing index and columns names means set it to None:

df = df.rename_axis(index=None, columns=None)print (df)         Column 1Apples        1.0Oranges       2.0Puppies       3.0Ducks         4.0

If MultiIndex in index only:

mux = pd.MultiIndex.from_arrays([['Apples', 'Oranges', 'Puppies', 'Ducks'],                                  list('abcd')],                                   names=['index name 1','index name 1'])df = pd.DataFrame(np.random.randint(10, size=(4,6)),                   index=mux,                   columns=list('ABCDEF')).rename_axis('col name', axis=1)print (df)col name                   A  B  C  D  E  Findex name 1 index name 1                  Apples       a             5  4  0  5  2  2Oranges      b             5  8  2  5  9  9Puppies      c             7  6  0  7  8  3Ducks        d             6  5  0  1  6  0

print (df.index.name)Noneprint (df.columns.name)col nameprint (df.index.names)['index name 1', 'index name 1']print (df.columns.names)['col name']

df1 = df.rename_axis(('foo','bar'))print (df1)col name     A  B  C  D  E  Ffoo     bar                  Apples  a    5  4  0  5  2  2Oranges b    5  8  2  5  9  9Puppies c    7  6  0  7  8  3Ducks   d    6  5  0  1  6  0df2 = df.rename_axis('baz', axis=1)print (df2)baz                        A  B  C  D  E  Findex name 1 index name 1                  Apples       a             5  4  0  5  2  2Oranges      b             5  8  2  5  9  9Puppies      c             7  6  0  7  8  3Ducks        d             6  5  0  1  6  0df2 = df.rename_axis(index=('foo','bar'), columns='baz')print (df2)baz          A  B  C  D  E  Ffoo     bar                  Apples  a    5  4  0  5  2  2Oranges b    5  8  2  5  9  9Puppies c    7  6  0  7  8  3Ducks   d    6  5  0  1  6  0

Removing index and columns names means set it to None:

df2 = df.rename_axis(index=(None,None), columns=None)print (df2)           A  B  C  D  E  FApples  a  6  9  9  5  4  6Oranges b  2  6  7  4  3  5Puppies c  6  3  6  3  5  1Ducks   d  4  9  1  3  0  5

For MultiIndex in index and columns is necessary working with .names instead .name and set by list or tuples:

mux1 = pd.MultiIndex.from_arrays([['Apples', 'Oranges', 'Puppies', 'Ducks'],                                  list('abcd')],                                   names=['index name 1','index name 1'])mux2 = pd.MultiIndex.from_product([list('ABC'),                                  list('XY')],                                   names=['col name 1','col name 2'])df = pd.DataFrame(np.random.randint(10, size=(4,6)), index=mux1, columns=mux2)print (df)col name 1                 A     B     C   col name 2                 X  Y  X  Y  X  Yindex name 1 index name 1                  Apples       a             2  9  4  7  0  3Oranges      b             9  0  6  0  9  4Puppies      c             2  4  6  1  4  4Ducks        d             6  6  7  1  2  8

Plural is necessary for check/set values:

print (df.index.name)Noneprint (df.columns.name)Noneprint (df.index.names)['index name 1', 'index name 1']print (df.columns.names)['col name 1', 'col name 2']

df1 = df.rename_axis(('foo','bar'))print (df1)col name 1   A     B     C   col name 2   X  Y  X  Y  X  Yfoo     bar                  Apples  a    2  9  4  7  0  3Oranges b    9  0  6  0  9  4Puppies c    2  4  6  1  4  4Ducks   d    6  6  7  1  2  8df2 = df.rename_axis(('baz','bak'), axis=1)print (df2)baz                        A     B     C   bak                        X  Y  X  Y  X  Yindex name 1 index name 1                  Apples       a             2  9  4  7  0  3Oranges      b             9  0  6  0  9  4Puppies      c             2  4  6  1  4  4Ducks        d             6  6  7  1  2  8df2 = df.rename_axis(index=('foo','bar'), columns=('baz','bak'))print (df2)baz          A     B     C   bak          X  Y  X  Y  X  Yfoo     bar                  Apples  a    2  9  4  7  0  3Oranges b    9  0  6  0  9  4Puppies c    2  4  6  1  4  4Ducks   d    6  6  7  1  2  8

Removing index and columns names means set it to None:

df2 = df.rename_axis(index=(None,None), columns=(None,None))print (df2)           A     B     C              X  Y  X  Y  X  YApples  a  2  0  2  5  2  0Oranges b  1  7  5  5  4  8Puppies c  2  4  6  3  6  5Ducks   d  9  6  3  9  7  0

And @Jeff solution:

df.index.names = ['foo','bar']df.columns.names = ['baz','bak']print (df)baz          A     B     C   bak          X  Y  X  Y  X  Yfoo     bar                  Apples  a    3  4  7  3  3  3Oranges b    1  2  5  8  1  0Puppies c    9  6  3  9  6  3Ducks   d    3  2  1  0  1  0


df.index.name should do the trick.

Python has a dir function that let's you query object attributes. dir(df.index) was helpful here.


matomo