3

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. enter image description here

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 itenter image description here

Jawad
  • 75
  • 1
  • 7

2 Answers2

11

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
ansev
  • 30,322
  • 5
  • 17
  • 31
  • I renamed the columns, it solved the issue! Thanks bud! – Jawad Aug 31 '19 at 13:06
  • Finally solved my problem after search for almost 1 hour. Thank you! – Steve Jun 05 '20 at 00:31
  • You are welcome @Steve We could use : `df = df.rename_axis(columns='')` https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rename_axis.html – ansev Jun 05 '20 at 09:00
4

To modify the DataFrame itself:

df.rename_axis(None, inplace=True)