[Python] Pandas sort_values() function on two columns isn't sorting the second column? [closed]

Discussão em 'Python' iniciado por Stack, Maio 2, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dataframe that I'm trying to sort by two columns - first by 'ROI', then by 'campaign_id'.

    The code is as follows:

    final_table.sort_values(by = ['ROI', 'campaign_id'], ascending = [False, False])

    The resulting dataframe is successfully sorted by ROI, but has this going on:

    campaign_id campaign_name ROI
    49 Campaign 49 172
    15 Campaign 15 172
    73 Campaign 73 172

    I've checked the datatypes; ROI is a float and campaign_id is an int. When I try to sort by just campaign_id, it sorts by those values just fine. What am I missing here?

    EDIT:

    Running the code final_table.dtypes gives the following output:

    campaign_id int64

    campaign_name object

    ROI float64

    dtype: object

    The campaign_id column contains no repeat values - it's an index that increments for each row of the original dataset. Here's some more of the output:

    campaign_id campaign_name ROI
    31 Campaign 31 176
    67 Campaign 67 176
    70 Campaign 70 175
    49 Campaign 49 172
    15 Campaign 15 172
    73 Campaign 73 172
    68 Campaign 68 170

    Continue reading...

Compartilhe esta Página