How to change the order of DataFrame columns?

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

    From August 2018:

    If your column names are too long to type then you could specify the new order through a list of integers with the positions:

    Data:

              0         1         2         3         4      mean
    0  0.397312  0.361846  0.719802  0.575223  0.449205  0.500678
    1  0.287256  0.522337  0.992154  0.584221  0.042739  0.485741
    2  0.884812  0.464172  0.149296  0.167698  0.793634  0.491923
    3  0.656891  0.500179  0.046006  0.862769  0.651065  0.543382
    4  0.673702  0.223489  0.438760  0.468954  0.308509  0.422683
    5  0.764020  0.093050  0.100932  0.572475  0.416471  0.389390
    6  0.259181  0.248186  0.626101  0.556980  0.559413  0.449972
    7  0.400591  0.075461  0.096072  0.308755  0.157078  0.207592
    8  0.639745  0.368987  0.340573  0.997547  0.011892  0.471749
    9  0.050582  0.714160  0.168839  0.899230  0.359690  0.438500
    

    Generic example:

    new_order = [3,2,1,4,5,0]
    print(df[df.columns[new_order]])  
    
              3         2         1         4      mean         0
    0  0.575223  0.719802  0.361846  0.449205  0.500678  0.397312
    1  0.584221  0.992154  0.522337  0.042739  0.485741  0.287256
    2  0.167698  0.149296  0.464172  0.793634  0.491923  0.884812
    3  0.862769  0.046006  0.500179  0.651065  0.543382  0.656891
    4  0.468954  0.438760  0.223489  0.308509  0.422683  0.673702
    5  0.572475  0.100932  0.093050  0.416471  0.389390  0.764020
    6  0.556980  0.626101  0.248186  0.559413  0.449972  0.259181
    7  0.308755  0.096072  0.075461  0.157078  0.207592  0.400591
    8  0.997547  0.340573  0.368987  0.011892  0.471749  0.639745
    9  0.899230  0.168839  0.714160  0.359690  0.438500  0.050582
    
          
    

    And for the specific case of OP's question:

    new_order = [-1,0,1,2,3,4]
    df = df[df.columns[new_order]]
    print(df)
    
           mean         0         1         2         3         4
    0  0.500678  0.397312  0.361846  0.719802  0.575223  0.449205
    1  0.485741  0.287256  0.522337  0.992154  0.584221  0.042739
    2  0.491923  0.884812  0.464172  0.149296  0.167698  0.793634
    3  0.543382  0.656891  0.500179  0.046006  0.862769  0.651065
    4  0.422683  0.673702  0.223489  0.438760  0.468954  0.308509
    5  0.389390  0.764020  0.093050  0.100932  0.572475  0.416471
    6  0.449972  0.259181  0.248186  0.626101  0.556980  0.559413
    7  0.207592  0.400591  0.075461  0.096072  0.308755  0.157078
    8  0.471749  0.639745  0.368987  0.340573  0.997547  0.011892
    9  0.438500  0.050582  0.714160  0.168839  0.899230  0.359690
    

    The main problem with this approach is that calling the same code multiple times will create different results each time, so one needs to be careful :)

提交回复
热议问题