[Python] How to change the order of DataFrame columns?

Discussão em 'Python' iniciado por Stack, Abril 1, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following DataFrame (df):

    import numpy as np
    import pandas as pd

    df = pd.DataFrame(np.random.rand(10, 5))


    I add more column(s) by assignment:

    df['mean'] = df.mean(1)


    How can I move the column mean to the front, i.e. set it as first column leaving the order of the other columns untouched?

    Continue reading...

Compartilhe esta Página