[Python] Move column by name to front of table in pandas

Discussão em 'Python' iniciado por Stack, Março 26, 2025.

  1. Stack

    Stack Membro Participativo

    Here is my df:

    Net Upper Lower Mid Zsore
    Answer option
    More than once a day 0% 0.22% -0.12% 2 65
    Once a day 0% 0.32% -0.19% 3 45
    Several times a week 2% 2.45% 1.10% 4 78
    Once a week 1% 1.63% -0.40% 6 65


    How can I move a column by name ("Mid") to the front of the table, index 0. This is what the result should look like:

    Mid Upper Lower Net Zsore
    Answer option
    More than once a day 2 0.22% -0.12% 0% 65
    Once a day 3 0.32% -0.19% 0% 45
    Several times a week 4 2.45% 1.10% 2% 78
    Once a week 6 1.63% -0.40% 1% 65


    My current code moves the column by index using df.columns.tolist() but I'd like to shift it by name.

    Continue reading...

Compartilhe esta Página