Renaming Columns

  • Comments posted to this topic are about the item Renaming Columns

  • I thought I wouldn't touch this question until it's been reviewed, but I couldn't resist.

    Clearly, option 3 wouldn't work, since the original column name is "BillingAmount", and not "BillAmount". In addition to that, since the Inplace parameter isn't set, option 3 would (if I understand things correctly) create a copy of the data frame with the column renamed.


    Just because you're right doesn't mean everybody else is wrong.

  • Two small gripes on this one.

    Option one won't work as it's renaming the wrong field.

    Option 3 also won't work (as mentioned above) as the originating field name is different to that in the data frame.

    From the given options, you would have to use the correct varioation of "BillingAmount" to get option 3 to work.

  • richardmgreen1 wrote:

    Option one won't work as it's renaming the wrong field.

    Option one should work, shouldn't it, since it sets the name of all the columns, but uses the old name for column 1 and the new name for column 2?


    Just because you're right doesn't mean everybody else is wrong.

  • Option 1 may well work, but in this instance, it's renaming the wrong field so is wrong for the question posted.

    (I know, I'm being really picky now).

  • richardmgreen1 wrote:

    Option 1 may well work, but in this instance, it's renaming the wrong field so is wrong for the question posted.

    (I know, I'm being really picky now).

    I really don't follow this. Option 1 will name the first column "BillMonth" (which is a no-op) , and the second column "InvoiceAmount". The columns parameter isn't a map, it's just a list of column names.


    Just because you're right doesn't mean everybody else is wrong.

  • Rune Bivrin wrote:

    I thought I wouldn't touch this question until it's been reviewed, but I couldn't resist.

    Clearly, option 3 wouldn't work, since the original column name is "BillingAmount", and not "BillAmount". In addition to that, since the Inplace parameter isn't set, option 3 would (if I understand things correctly) create a copy of the data frame with the column renamed.

    +1

    Thanks Rune, explained exactly, this can be verified by running the code.

  • Option 1 works only if you replace the {} with [] as follows:

    df.columns = ['BillMonth', 'InvoiceAmount']

  • Aaron N. Cutshall wrote:

    Option 1 works only if you replace the {} with [] as follows:

    df.columns = ['BillMonth', 'InvoiceAmount']

    True, dat! So, we're down to the missing alternative of "None", I suppose...


    Just because you're right doesn't mean everybody else is wrong.

  • In Python Pandas (Python v2.7.13), option #1 works with both { } and [ ].

    In addition, option #3 without the Inplace=True parameter will do nothing and the original df will not change.

    Without the Inplace parameter, you can only make a new copy of the original df e.g like this:

    df_new = df.rename (columns = {'BillingAmount': 'InvoiceAmount'})
    print df_new #The column BillingAmount is renamed to InvoiceAmount

    The explanation for QotD 2019-12-18 is very vague. Answer #5 is not true.

    • This reply was modified 4 years, 4 months ago by  George Vobr.
    • This reply was modified 4 years, 4 months ago by  George Vobr.
    • This reply was modified 4 years, 4 months ago by  George Vobr.

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply