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