Can I assign a reset index a name?

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?

0

4 Answers

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

2

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 
0

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 
0

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like