noob
noob

Reputation: 3811

How to get rid of name of column/axis name showing on top of /above index in dataframe pandas python: Renaming axis

Current dataframe

enter image description here


Expected output


enter image description here

when I do df.columns() this is what I get:

[IN] df.columns
[OUT] Index(['Col1', 'Col2', 'Col3', 'Col4'],
      dtype='object', name='SKU')

When I do df.columns SKU is shown as name of columns as shown above.

Dtypes are as below

 [IN] df.dtypes
 [OUT]

SKU
Col1          float64
Col2          float64
Col3          float64
Col4          float64
dtype: object

Date is the index.

  [IN]  df.index.name
  [OUT] 'Date'

What I want

I want to remove SKU from my dataframe. It is extra on top of date/index in the dataframe as shown in the image.

Upvotes: 2

Views: 967

Answers (3)

N. P.
N. P.

Reputation: 521

You have to swap the columns and index names and then remove index name:

df.columns.name = df.index.name
df.index.name = None

Upvotes: 0

Hossein Amini
Hossein Amini

Reputation: 35

you can do it 1st by removing from csv file but if you want to do via using code just write it down

df = pd.DataFrame({'month': [1, 4, 7, 10],
                   'year': [2012, 2014, 2013, 2014],
                   'sale': [55, 40, 84, 31]})
df
   month  year  sale
0      1  2012    55
1      4  2014    40
2      7  2013    84
3     10  2014    31

to

df.set_index('month')
       year  sale
month
1      2012    55
4      2014    40
7      2013    84
10     2014    31

Visit https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.set_index.html

Upvotes: 0

jezrael
jezrael

Reputation: 863166

You can remove columns name SKU by DataFrame.rename_axis:

df = df.rename_axis(None, axis=1)

But I think empty row is not removed, need also remove index name date:

df = df.rename_axis(None)

Both together:

df = df.rename_axis(index=None, columns=None)

Upvotes: 3

Related Questions