How to remove the index name in pandas dataframe?

浪尽此生 提交于 2020-06-13 11:51:14

问题


In my dataframe, I get a '2' written over my index column's name. when I check for the columns name it doesn't show up there but as df.columns give this as output. I don't know how to remove that '2' from my dataset.

I have tried removing index name but it hasn't solved my issue.

 df.columns ==> Output
     Index(['name', 'census 1981', 'census 1998', 'estimate 2000',
       'calculation 2010', 'annual growth', 'latitude', 'longitude',
       'parent division', 'name variants'],
      dtype='object', name=2)

I expect only the index with its name...not including that freaking '2' over it


回答1:


you need change the name of columns, not index!

df.columns.name=''

Example to understand it:

df=pd.DataFrame()
df['a']=[1,2,3]
df.columns.name='name column'
df.index.name='name index'
df

Output:

name column  a
name index  
0            1
1            2
2            3

Now doing:

df.columns.name=''

Output:

           a
name index  
0          1
1          2
2          3



回答2:


To modify the DataFrame itself:

df.rename_axis(None, inplace=True)


来源:https://stackoverflow.com/questions/57738129/how-to-remove-the-index-name-in-pandas-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!