How to change the order of DataFrame columns?

前端 未结 30 2047
南旧
南旧 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条回答
  •  萌比男神i
    2020-11-22 02:19

    You could do the following (borrowing parts from Aman's answer):

    cols = df.columns.tolist()
    cols.insert(0, cols.pop(-1))
    
    cols
    >>>['mean', 0L, 1L, 2L, 3L, 4L]
    
    df = df[cols]
    

提交回复
热议问题