How to change the order of DataFrame columns?

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

    In your case,

    df = df.reindex(columns=['mean',0,1,2,3,4])
    

    will do exactly what you want.

    In my case (general form):

    df = df.reindex(columns=sorted(df.columns))
    df = df.reindex(columns=(['opened'] + list([a for a in df.columns if a != 'opened']) ))
    

提交回复
热议问题