2

I have a data-frame (df):

       0       1         2            3                    4     5
0   6894   JONES     STEVE            D  2017-01-03 00:00:00  None
1  13555   SMITH     JENNY            E  2017-04-01 00:00:00  None

I have a header called new_header that I am applying to the data-frame which looks like:

0                     Identifier
1                        Surname
2                  First name(s)
3                 Transferred to
4                  Transfer Date
5                         Status

I apply new_header with the following:

df.columns = new_header

and I get the following output:

0               Identifier Surname  ...          Transfer Date Status
0                     6894   JONES  ...    2017-01-03 00:00:00   None
1                    13555   SMITH  ...    2017-04-01 00:00:00   None

In the top left corner of df you can see a 0. How can I remove the 0 so my data-frame looks like:

                Identifier Surname  ...          Transfer Date Status
0                     6494   JONES  ...    2017-01-03 00:00:00   None
1                    13595   SMITH  ...    2017-04-01 00:00:00   None
3
  • 2
    what if you do df.columns = new_header.values ? Commented May 9, 2019 at 15:29
  • 2
    df.columns.name = None should do it Commented May 9, 2019 at 15:30
  • Yes! Thank you so much! Commented May 9, 2019 at 15:30

2 Answers 2

3

You are attempting to rename columns with a pandas.Series object. When doing so, the name of the Series translates to the name of the resulting pandas.Index object. You can solve this in various ways.

Assign to df.columns something that doesn't have a name as suggested by @anky_91

df.columns = new_header.values

df

   Identifier Surname First name(s) Transferred to        Transfer Date Status
0        6894   JONES         STEVE              D  2017-01-03 00:00:00   None
1       13555   SMITH         JENNY              E  2017-04-01 00:00:00   None

Assign to df.columns a series whose name is None

df.columns = new_header.rename(None)

df

   Identifier Surname First name(s) Transferred to        Transfer Date Status
0        6894   JONES         STEVE              D  2017-01-03 00:00:00   None
1       13555   SMITH         JENNY              E  2017-04-01 00:00:00   None

Rename the axis after the fact

df.columns = new_header
df.rename_axis(None, 1)

   Identifier Surname First name(s) Transferred to        Transfer Date Status
0        6894   JONES         STEVE              D  2017-01-03 00:00:00   None
1       13555   SMITH         JENNY              E  2017-04-01 00:00:00   None

Use rename with the new_header as a renaming mapping

This assumes the index of new_header lines up with what is being renamed in df.
Also, I had to get tricky and make sure that they were the same type.

df.rename(columns=int).rename(columns=new_header)

   Identifier Surname First name(s) Transferred to        Transfer Date Status
0        6894   JONES         STEVE              D  2017-01-03 00:00:00   None
1       13555   SMITH         JENNY              E  2017-04-01 00:00:00   None
Sign up to request clarification or add additional context in comments.

Comments

0

You can get or set the index by name

df.index.name = ''

see the docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.