a1234
a1234

Reputation: 821

Pandas rename index

I have the following dataframe, where I want to rename the index fromsummary to id:

summary  student  count 
0        error    6
1        yes      1
2        no       1
3        other    9

I have tried: newdf = df.reset_index().rename(columns={df.index.name:'foo'}) which gives:

summary  index    student  count    
0        0        error   6
1        1        yes     1
2        2        no      1
3        3        other   9

I have also tried: df.index.rename('foo', inplace = True) which gives:

 summary     student  count
 foo        
 0           error    6
 1           yes      1
 2           no       1
 3           other    9

I have also tried: df.rename_axis('why', inplace = True) which gives:

 summary     student  count
 why        
 0           error    6
 1           yes      1
 2           no       1
 3           other    9

When I do df.dtypes:

summary
student object
count   init64
dtype:  object

What I would like:

id  student  count 
0   error    6
1   yes      1
2   no       1
3   other    9

OR:

    student  count 
0   error    6
1   yes      1
2   no       1
3   other    9

Upvotes: 6

Views: 33166

Answers (3)

Yuca
Yuca

Reputation: 6091

you need to access the index's properties

df.index.name = 'id'

original

         student  count
summary               
0         error      6
1           yes      1
2            no      1
3         other      9

fixed df:

    student  count
id               
0    error      6
1      yes      1
2       no      1
3    other      9

Update: seems like you had a name for the column's index. You should remove it with

df.columns.names = ''

Upvotes: 6

ALollz
ALollz

Reputation: 59549

You need to remove the column name:

df.rename_axis(None, axis=1).rename_axis('id', axis=0)
##if pd.__version__ == 0.24.0 
#df.rename_axis([None], axis=1).rename_axis('id')

The problem is that 'summary' is your column name. When there is no index name, the column name is placed directly above the index, which can be misleading:

import pandas as pd
df = pd.DataFrame([[1]*2]*4, columns=['A', 'B'])
df.columns.name = 'col_name'
print(df)

#col_name  A  B
#0         1  1
#1         1  1
#2         1  1
#3         1  1

When you then try to add an index name, it becomes clear that 'col_name' was really the column name.

df.index.name = 'idx_name'
print(df)

#col_name  A  B
#idx_name      
#0         1  1
#1         1  1
#2         1  1
#3         1  1

There is no ambiguity though: when you have an index name, the columns are raised one level, which allows you to distinguish between an index name and a column name.

df = pd.DataFrame([[1]*2]*4, columns=['A', 'B'])
df.index.name = 'idx_name'
print(df)

#          A  B
#idx_name      
#0         1  1
#1         1  1
#2         1  1
#3         1  1

Upvotes: 13

Mohit Musaddi
Mohit Musaddi

Reputation: 143

First you can drop the column:

df = df.drop('summary', axis=1)
df['id'] = np.arange(df.shape[0])
df.set_index('id', inplace=True)

Then you can get the desired result.

Upvotes: 1

Related Questions