Franco Piccolo
Franco Piccolo

Reputation: 7410

How to drop name from df.columns?

I would like to know how to remove the name from the df.columns attribute:

Let's say I have the following dataframe with df.columns:

df = pd.DataFrame({'a':[1,2,3], 'b':[4,5,6]})
df

    a   b
0   1   4
1   2   5
2   3   6

df.columns

Index(['a', 'b'], dtype='object')

And then if I add a name to the columns attribute then I get the dataframe:

df.columns.name = 'test'
df.columns

Index(['a', 'b'], dtype='object', name='test')

df

test    a   b
   0    1   4
   1    2   5
   2    3   6

Is there a way to drop the name test that appears above the index?

Upvotes: 1

Views: 1161

Answers (4)

HowdyDude
HowdyDude

Reputation: 483

df.columns.name=None Would be my solutions

Upvotes: 1

Maroca
Maroca

Reputation: 552

Not sure why you would name the attribute to then drop its name, but you can use the same code used to name it:

df.columns.name=None

Upvotes: -1

jpp
jpp

Reputation: 164663

You can delete the attribute via del:

df.columns.name = 'test'
print(df.columns)
# Index(['a', 'b'], dtype='object', name='test')

del df.columns.name
print(df.columns)
# Index(['a', 'b'], dtype='object')

Upvotes: 2

BENY
BENY

Reputation: 323226

You can using rename_axis

df.rename_axis(None,axis=1)
Out[68]: 
   a  b
0  1  4
1  2  5
2  3  6

Upvotes: 1

Related Questions