How to change the order of DataFrame columns?

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

    A pretty straightforward solution that worked for me is to use .reindex on df.columns:

    df=df[df.columns.reindex(['mean',0,1,2,3,4])[0]]
    
    0 讨论(0)
  • 2020-11-22 02:03

    This question has been answered before but reindex_axis is deprecated now so I would suggest to use:

    df.reindex(sorted(df.columns), axis=1)
    
    0 讨论(0)
  • 2020-11-22 02:05

    How about using "T"?

    df.T.reindex(['mean',0,1,2,3,4]).T
    
    0 讨论(0)
  • 2020-11-22 02:06

    Here is a function to do this for any number of columns.

    def mean_first(df):
        ncols = df.shape[1]        # Get the number of columns
        index = list(range(ncols)) # Create an index to reorder the columns
        index.insert(0,ncols)      # This puts the last column at the front
        return(df.assign(mean=df.mean(1)).iloc[:,index]) # new df with last column (mean) first
    
    0 讨论(0)
  • 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']) ))
    
    0 讨论(0)
  • 2020-11-22 02:07
    import numpy as np
    import pandas as pd
    df = pd.DataFrame()
    column_names = ['x','y','z','mean']
    for col in column_names: 
        df[col] = np.random.randint(0,100, size=10000)
    

    You can try out the following solutions :

    Solution 1:

    df = df[ ['mean'] + [ col for col in df.columns if col != 'mean' ] ]
    

    Solution 2:


    df = df[['mean', 'x', 'y', 'z']]
    

    Solution 3:

    col = df.pop("mean")
    df = df.insert(0, col.name, col)
    

    Solution 4:

    df.set_index(df.columns[-1], inplace=True)
    df.reset_index(inplace=True)
    

    Solution 5:

    cols = list(df)
    cols = [cols[-1]] + cols[:-1]
    df = df[cols]
    

    solution 6:

    order = [1,2,3,0] # setting column's order
    df = df[[df.columns[i] for i in order]]
    

    Time Comparison:

    Solution 1:

    CPU times: user 1.05 ms, sys: 35 µs, total: 1.08 ms Wall time: 995 µs

    Solution 2:

    CPU times: user 933 µs, sys: 0 ns, total: 933 µs Wall time: 800 µs

    Solution 3:

    CPU times: user 0 ns, sys: 1.35 ms, total: 1.35 ms Wall time: 1.08 ms

    Solution 4:

    CPU times: user 1.23 ms, sys: 45 µs, total: 1.27 ms Wall time: 986 µs

    Solution 5:

    CPU times: user 1.09 ms, sys: 19 µs, total: 1.11 ms Wall time: 949 µs

    Solution 6:

    CPU times: user 955 µs, sys: 34 µs, total: 989 µs Wall time: 859 µs

    0 讨论(0)
提交回复
热议问题