Remove Index Name in Pandas

Remove index name in pandas

Use del df.index.name

In [16]: df
Out[16]:
Column 1
foo
Apples 1
Oranges 2
Puppies 3
Ducks 4

In [17]: del df.index.name

In [18]: df
Out[18]:
Column 1
Apples 1
Oranges 2
Puppies 3
Ducks 4

Pandas - remove the label of the column index

New answer for pandas 1.x, submitted by Ian Logie

df.columns.name = None

 

Old Answer from Oct 2018

Simply delete the name of the columns:

del df.columns.name

Also, note that df.index.names = [''] is not quite the same as del df.index.name.

How to remove the index name in pandas dataframe?

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

Removing index name from df created with pivot_table()

You can use rename_axis:

df = df.rename_axis(None, axis=1)  
# df.columns.name = None

# To remove index label
df = df.rename_axis(None, axis=0)
# df.index.name = None

Removing index column in pandas when reading a csv

DataFrames and Series always have an index. Although it displays alongside the column(s), it is not a column, which is why del df['index'] did not work.

If you want to replace the index with simple sequential numbers, use df.reset_index().

To get a sense for why the index is there and how it is used, see e.g. 10 minutes to Pandas.

Pandas can't remove column name on the index column

It will work, you can try assign it back ,also it is column name not index name

df = df.rename_axis(None, axis="columns")

Remove higher level index names after pivot

You can use Index.map() with f-string, as follows:

df_.columns = df_.columns.map(lambda x: f'{x[0]}_{x[1]}')

Using this way, you have the freedom to arrange the sequence of the combined words from the MultiIndex as you wish. E.g. if you want to get the city name first then the word 'temperature' (e.g. Berlin_temperature instead), you can just reverse the sequence of x[0] and x[1] in the f-string above.

Result:

print(df_)

temperature_Berlin temperature_New-York temperature_Paris temperature_Rio temperature_Sydney temperature_Tokyo humidity_Berlin humidity_New-York humidity_Paris humidity_Rio humidity_Sydney humidity_Tokyo
date
2020-01-01 00:00:00 NaN NaN 22.8 NaN 24.7 28.8 NaN NaN 1.0 NaN 0.9 0.0
2020-01-02 00:00:00 20.2 21.5 21.6 21.6 4.3 21.5 0.5 0.5 1.0 0.4 0.4 0.0
2020-01-03 00:00:00 NaN 17.3 24.4 NaN 11.3 22.7 NaN 0.4 0.1 NaN 0.0 0.5

removing the name of a pandas dataframe index after appending a total row to a dataframe

That's the columns' name.

In [11]: df = pd.DataFrame([[1, 2]], columns=['A', 'B'])

In [12]: df
Out[12]:
A B
0 1 2

In [13]: df.columns.name = 'XX'

In [14]: df
Out[14]:
XX A B
0 1 2

You can set it to None to clear it.

In [15]: df.columns.name = None

In [16]: df
Out[16]:
A B
0 1 2

An alternative, if you wanted to keep it, is to give the index a name:

In [21]: df.columns.name = "XX"

In [22]: df.index.name = "index"

In [23]: df
Out[23]:
XX A B
index
0 1 2


Related Topics



Leave a reply



Submit