How to change the order of DataFrame columns?

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

    Just type the column name you want to change, and set the index for the new location.

    def change_column_order(df, col_name, index):
        cols = df.columns.tolist()
        cols.remove(col_name)
        cols.insert(index, col_name)
        return df[cols]
    

    For your case, this would be like:

    df = change_column_order(df, 'mean', 0)
    

提交回复
热议问题