Can I assign a reset index a name?

PythonPandas

Python Problem Overview


Normally when a dataframe undergoes a reset_index() the new column is assigned the name index or level_i depending on the level.

Is it possible to assign the new column a name?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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

Solution 3 - Python

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

Solution 4 - Python

If you're using reset_index() to go from a Series to a DataFrame you can name the column like this

my_series.rename('Example').reset_index()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDemetri PananosView Question on Stackoverflow
Solution 1 - PythonEdChumView Answer on Stackoverflow
Solution 2 - PythonigorkfView Answer on Stackoverflow
Solution 3 - Pythonarno_vView Answer on Stackoverflow
Solution 4 - PythonpyjamasView Answer on Stackoverflow