Can I assign a reset index a name?

You could do this (Jan of 2020):

df = df.reset_index().rename(columns={'index': 'bar'})
print(df)
   bar         0         1         2
0    0 -2.845811 -0.182439 -0.526785
1    1 -0.112547  0.661461  0.558452
2    2  0.587060 -1.232262 -0.997973
3    3 -1.009378 -0.062442  0.125875
4    4 -1.129376  3.282447 -0.403731

You can call rename on the returned df from reset_index:

In [145]:
# create a df
df = pd.DataFrame(np.random.randn(5,3))
df

Out[145]:
          0         1         2
0 -2.845811 -0.182439 -0.526785
1 -0.112547  0.661461  0.558452
2  0.587060 -1.232262 -0.997973
3 -1.009378 -0.062442  0.125875
4 -1.129376  3.282447 -0.403731

Set the index name

In [146]:    
df.index = df.index.set_names(['foo'])
df

Out[146]:
            0         1         2
foo                              
0   -2.845811 -0.182439 -0.526785
1   -0.112547  0.661461  0.558452
2    0.587060 -1.232262 -0.997973
3   -1.009378 -0.062442  0.125875
4   -1.129376  3.282447 -0.403731

call reset_index and chain with rename:

In [147]:
df.reset_index().rename(columns={df.index.name:'bar'})

Out[147]:
   bar         0         1         2
0    0 -2.845811 -0.182439 -0.526785
1    1 -0.112547  0.661461  0.558452
2    2  0.587060 -1.232262 -0.997973
3    3 -1.009378 -0.062442  0.125875
4    4 -1.129376  3.282447 -0.403731

Thanks to @ayhan

alternatively you can use rename_axis to rename the index prior to reset_index:

In [149]:
df.rename_axis('bar').reset_index()

Out[149]:
   bar         0         1         2
0    0 -2.845811 -0.182439 -0.526785
1    1 -0.112547  0.661461  0.558452
2    2  0.587060 -1.232262 -0.997973
3    3 -1.009378 -0.062442  0.125875
4    4 -1.129376  3.282447 -0.403731

or just overwrite the index name directly first:

df.index.name = 'bar'

and then call reset_index


If you are seeking one-liners that return a new DataFrame, use assign. Here's an example:

>>> df = pd.DataFrame({"a": [4.4, 2.2], "b": 8}, index=[10, 20])
>>> df
      a  b
10  4.4  8
20  2.2  8

Assign a bar Series with the index values, but keep the original index:

>>> df.assign(bar=df.index)
      a  b  bar
10  4.4  8   10
20  2.2  8   20

Similar, but drop the index:

>>> df.assign(bar=df.index).reset_index(drop=True)
     a  b  bar
0  4.4  8   10
1  2.2  8   20

For a Series you can specify the name directly. E.g.:

>>> df.groupby('s1').size().reset_index(name='new_name')
  s1  new_name
0  b         1
1  r         1
2  s         1

Tags:

Python

Pandas