How to change the order of DataFrame columns?

前端 未结 30 2356
南旧
南旧 2020-11-22 01:24

I have the following DataFrame (df):

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.rand(10, 5))
30条回答
  •  忘掉有多难
    2020-11-22 02:12

    You could also do something like this:

    df = df[['mean', '0', '1', '2', '3']]
    

    You can get the list of columns with:

    cols = list(df.columns.values)
    

    The output will produce:

    ['0', '1', '2', '3', 'mean']
    

    ...which is then easy to rearrange manually before dropping it into the first function

提交回复
热议问题