How to change the order of DataFrame columns?

前端 未结 30 1577
南旧
南旧 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:14

    set():

    A simple approach is using set(), in particular when you have a long list of columns and do not want to handle them manually:

    cols = list(set(df.columns.tolist()) - set(['mean']))
    cols.insert(0, 'mean')
    df = df[cols]
    

提交回复
热议问题