Renaming Columns

  • Steve Jones - SSC Editor

    SSC Guru

    Points: 720940

    Comments posted to this topic are about the item Renaming Columns

  • Rune Bivrin

    SSCertifiable

    Points: 7856

    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.

  • richardmgreen1

    SSCrazy Eights

    Points: 9949

    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.

  • Rune Bivrin

    SSCertifiable

    Points: 7856

    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.

  • richardmgreen1

    SSCrazy Eights

    Points: 9949

    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).

  • Rune Bivrin

    SSCertifiable

    Points: 7856

    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.

  • George Vobr

    SSCrazy Eights

    Points: 9248

    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.

  • Aaron N. Cutshall

    SSCrazy Eights

    Points: 8812

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

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

  • Rune Bivrin

    SSCertifiable

    Points: 7856

    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.

  • George Vobr

    SSCrazy Eights

    Points: 9248

    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 9 months, 1 week ago by  George Vobr.
    • This reply was modified 9 months, 1 week ago by  George Vobr.
    • This reply was modified 9 months, 1 week ago by  George Vobr.

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

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