[Python] Pandas: How to custom-sort on multiple columns? [duplicate]

Discussão em 'Python' iniciado por Stack, Fevereiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    I have a pandas dataframe with data like:

    +-----------+-----------------+---------+
    | JOB-NAME | Status | SLA |
    +-----------+-----------------+---------+
    | job_1 | YET_TO_START | --- |
    | job_3 | COMPLETED | MET |
    | job_4 | RUNNING | MET |
    | job_2 | YET_TO_START | LATE |
    | job_6 | RUNNING | LATE |
    | job_5 | FAILED | LATE |
    | job_7 | YET_TO_START | --- |
    | job_8 | COMPLETED | NOT_MET |
    +-----------+-----------------+---------+


    I need to sort this table based on the Status and SLA states, like for Status: FAILED will be top on the table, then YET_TO_START, then RUNNING, and finally COMPLETED. Similarly for SLA the order will be LATE, ---, NOT_MET, and MET. Like this:

    +-----------+-----------------+---------+
    | JOB-NAME | Status | SLA |
    +-----------+-----------------+---------+
    | job_5 | FAILED | LATE |
    | job_2 | YET_TO_START | LATE |
    | job_1 | YET_TO_START | --- |
    | job_7 | YET_TO_START | --- |
    | job_6 | RUNNING | LATE |
    | job_4 | RUNNING | MET |
    | job_8 | COMPLETED | NOT_MET |
    | job_3 | COMPLETED | MET |
    +-----------+-----------------+---------+


    I am able to do this custom sorting priority-based only on single column Status, but unable to do for multiple columns.

    sort_order_dict = {"FAILED":0, "YET_TO_START":1, "RUNNING":2, "COMPLETED":3}
    joined_df = joined_df.sort_values(by=['status'], key=lambda x: x.map(sort_order_dict))


    A solution is given here, but its for single column, not multiple column.

    Continue reading...

Compartilhe esta Página