Python change DataFrame column order

Posted by billborric on Sun, 16 Jan 2022 23:06:21 +0100

Article launch and subsequent updates: https://mwhls.top/3390.html
For new updates, please go to mwhls.top see.
No figure / no directory / format error / more relevant, please go to the article launch page above.

stackoverflow hot issues directory

If you have any translation questions, please comment. Thank you.

How do I change the order of DataFrame columns?

  • Timmie asked:

    • For the following DataFrame(df):

    • import numpy as np
      import pandas as pd
      #
      df = pd.DataFrame(np.random.rand(10, 5))
      
    • I added a new column:

    • df['mean'] = df.mean(1)
      
    • How do I move the mean column to the beginning? Or how to take the mean column as the first column and move back without changing the order of other columns?

  • Answers:

    • Aman - vote: 1144

      • A simple way is to change the arrangement of the dataframe in the form of a list. You can arrange the columns in different ways as needed.

      • For example, for the following dataframe:

      • In [6]: df
        Out[6]:
                  0         1         2         3         4      mean
        0  0.445598  0.173835  0.343415  0.682252  0.582616  0.445543
        1  0.881592  0.696942  0.702232  0.696724  0.373551  0.670208
        2  0.662527  0.955193  0.131016  0.609548  0.804694  0.632596
        3  0.260919  0.783467  0.593433  0.033426  0.512019  0.436653
        4  0.131842  0.799367  0.182828  0.683330  0.019485  0.363371
        5  0.498784  0.873495  0.383811  0.699289  0.480447  0.587165
        6  0.388771  0.395757  0.745237  0.628406  0.784473  0.588529
        7  0.147986  0.459451  0.310961  0.706435  0.100914  0.345149
        8  0.394947  0.863494  0.585030  0.565944  0.356561  0.553195
        9  0.689260  0.865243  0.136481  0.386582  0.730399  0.561593
        # 
        In [7]: cols = df.columns.tolist()
        # 
        In [8]: cols
        Out[8]: [0L, 1L, 2L, 3L, 4L, 'mean']
        
      • Rearrange these cols the way you want them to. Here is how I move the last column element to the first column:

      • In [12]: cols = cols[-1:] + cols[:-1]
        # 
        In [13]: cols
        Out[13]: ['mean', 0L, 1L, 2L, 3L, 4L]
        
      • The sorted dataframe is as follows:

      • In [16]: df = df[cols]  #    OR    df = df.ix[:, cols]
        # 
        In [17]: df
        Out[17]:
               mean         0         1         2         3         4
        0  0.445543  0.445598  0.173835  0.343415  0.682252  0.582616
        1  0.670208  0.881592  0.696942  0.702232  0.696724  0.373551
        2  0.632596  0.662527  0.955193  0.131016  0.609548  0.804694
        3  0.436653  0.260919  0.783467  0.593433  0.033426  0.512019
        4  0.363371  0.131842  0.799367  0.182828  0.683330  0.019485
        5  0.587165  0.498784  0.873495  0.383811  0.699289  0.480447
        6  0.588529  0.388771  0.395757  0.745237  0.628406  0.784473
        7  0.345149  0.147986  0.459451  0.310961  0.706435  0.100914
        8  0.553195  0.394947  0.863494  0.585030  0.565944  0.356561
        9  0.561593  0.689260  0.865243  0.136481  0.386582  0.730399
        
    • freddygv - vote: 675

      • This will:

      • df = df[['mean', '0', '1', '2', '3']]
        
      • The following code is used to get a list of columns:

      • cols = list(df.columns.values)
        
      • Output:

      • ['0', '1', '2', '3', 'mean']
        
      • It's very convenient.

    • fixxxer - vote: 367

      • Just process the column names in the order you want them to:

      • In [39]: df
        Out[39]: 
                  0         1         2         3         4  mean
        0  0.172742  0.915661  0.043387  0.712833  0.190717     1
        1  0.128186  0.424771  0.590779  0.771080  0.617472     1
        2  0.125709  0.085894  0.989798  0.829491  0.155563     1
        3  0.742578  0.104061  0.299708  0.616751  0.951802     1
        4  0.721118  0.528156  0.421360  0.105886  0.322311     1
        5  0.900878  0.082047  0.224656  0.195162  0.736652     1
        6  0.897832  0.558108  0.318016  0.586563  0.507564     1
        7  0.027178  0.375183  0.930248  0.921786  0.337060     1
        8  0.763028  0.182905  0.931756  0.110675  0.423398     1
        9  0.848996  0.310562  0.140873  0.304561  0.417808     1
        # 
        In [40]: df = df[['mean', 4,3,2,1]]
        
      • After processing, the 'mean' column will be at the beginning:

      • In [41]: df
        Out[41]: 
           mean         4         3         2         1
        0     1  0.190717  0.712833  0.043387  0.915661
        1     1  0.617472  0.771080  0.590779  0.424771
        2     1  0.155563  0.829491  0.989798  0.085894
        3     1  0.951802  0.616751  0.299708  0.104061
        4     1  0.322311  0.105886  0.421360  0.528156
        5     1  0.736652  0.195162  0.224656  0.082047
        6     1  0.507564  0.586563  0.318016  0.558108
        7     1  0.337060  0.921786  0.930248  0.375183
        8     1  0.423398  0.110675  0.931756  0.182905
        9     1  0.417808  0.304561  0.140873  0.310562
        

How to change the order of DataFrame columns?

  • Timmie asked:

    • I have the following DataFrame (df):
      For the following DataFrame(df):

    • import numpy as np
      import pandas as pd
      # 
      df = pd.DataFrame(np.random.rand(10, 5))
      
    • I add more column(s) by assignment:

      I added a new column:

    • df['mean'] = df.mean(1)
      
    • How can I move the column mean to the front, i.e. set it as first column leaving the order of the other columns untouched?
      How do I move the mean column to the beginning? Or how to take the mean column as the first column and move back without changing the order of other columns?

  • Answers:

    • Aman - vote: 1144

      • One easy way would be to reassign the dataframe with a list of the columns, rearranged as needed.
        A simple way is to change the arrangement of the dataframe in the form of a list. You can arrange the columns in different ways as needed.

      • This is what you have now:
        For example, for the following dataframe:

      • In [6]: df
        Out[6]:
                  0         1         2         3         4      mean
        0  0.445598  0.173835  0.343415  0.682252  0.582616  0.445543
        1  0.881592  0.696942  0.702232  0.696724  0.373551  0.670208
        2  0.662527  0.955193  0.131016  0.609548  0.804694  0.632596
        3  0.260919  0.783467  0.593433  0.033426  0.512019  0.436653
        4  0.131842  0.799367  0.182828  0.683330  0.019485  0.363371
        5  0.498784  0.873495  0.383811  0.699289  0.480447  0.587165
        6  0.388771  0.395757  0.745237  0.628406  0.784473  0.588529
        7  0.147986  0.459451  0.310961  0.706435  0.100914  0.345149
        8  0.394947  0.863494  0.585030  0.565944  0.356561  0.553195
        9  0.689260  0.865243  0.136481  0.386582  0.730399  0.561593
        # 
        In [7]: cols = df.columns.tolist()
        # 
        In [8]: cols
        Out[8]: [0L, 1L, 2L, 3L, 4L, 'mean']
        
      • Rearrange cols in any way you want. This is how I moved the last element to the first position:
        Rearrange these cols the way you want them to. Here is how I move the last column element to the first column:

      • In [12]: cols = cols[-1:] + cols[:-1]
        #
        In [13]: cols
        Out[13]: ['mean', 0L, 1L, 2L, 3L, 4L]
        
      • Then reorder the dataframe like this:
        The sorted dataframe is as follows:

      • In [16]: df = df[cols]  #    OR    df = df.ix[:, cols]
        # 
        In [17]: df
        Out[17]:
               mean         0         1         2         3         4
        0  0.445543  0.445598  0.173835  0.343415  0.682252  0.582616
        1  0.670208  0.881592  0.696942  0.702232  0.696724  0.373551
        2  0.632596  0.662527  0.955193  0.131016  0.609548  0.804694
        3  0.436653  0.260919  0.783467  0.593433  0.033426  0.512019
        4  0.363371  0.131842  0.799367  0.182828  0.683330  0.019485
        5  0.587165  0.498784  0.873495  0.383811  0.699289  0.480447
        6  0.588529  0.388771  0.395757  0.745237  0.628406  0.784473
        7  0.345149  0.147986  0.459451  0.310961  0.706435  0.100914
        8  0.553195  0.394947  0.863494  0.585030  0.565944  0.356561
        9  0.561593  0.689260  0.865243  0.136481  0.386582  0.730399
        
    • freddygv - vote: 675

      • You could also do something like this:
        This will:

      • df = df[['mean', '0', '1', '2', '3']]
        
      • You can get the list of columns with:
        The following code is used to get a list of columns:

      • cols = list(df.columns.values)
        
      • The output will produce:
        Output:

      • ['0', '1', '2', '3', 'mean']
        
      • ...which is then easy to rearrange manually before dropping it into the first function
        It's very convenient.

    • fixxxer - vote: 367

      • Just assign the column names in the order you want them:
        Just process the column names in the order you want them to:

      • In [39]: df
        Out[39]: 
                  0         1         2         3         4  mean
        0  0.172742  0.915661  0.043387  0.712833  0.190717     1
        1  0.128186  0.424771  0.590779  0.771080  0.617472     1
        2  0.125709  0.085894  0.989798  0.829491  0.155563     1
        3  0.742578  0.104061  0.299708  0.616751  0.951802     1
        4  0.721118  0.528156  0.421360  0.105886  0.322311     1
        5  0.900878  0.082047  0.224656  0.195162  0.736652     1
        6  0.897832  0.558108  0.318016  0.586563  0.507564     1
        7  0.027178  0.375183  0.930248  0.921786  0.337060     1
        8  0.763028  0.182905  0.931756  0.110675  0.423398     1
        9  0.848996  0.310562  0.140873  0.304561  0.417808     1
        # 
        In [40]: df = df[['mean', 4,3,2,1]]
        
      • Now, 'mean' column comes out in the front:
        After processing, the 'mean' column will be at the beginning:

      • In [41]: df
        Out[41]: 
           mean         4         3         2         1
        0     1  0.190717  0.712833  0.043387  0.915661
        1     1  0.617472  0.771080  0.590779  0.424771
        2     1  0.155563  0.829491  0.989798  0.085894
        3     1  0.951802  0.616751  0.299708  0.104061
        4     1  0.322311  0.105886  0.421360  0.528156
        5     1  0.736652  0.195162  0.224656  0.082047
        6     1  0.507564  0.586563  0.318016  0.558108
        7     1  0.337060  0.921786  0.930248  0.375183
        8     1  0.423398  0.110675  0.931756  0.182905
        9     1  0.417808  0.304561  0.140873  0.310562
        

Topics: Python DataFrame StackOverflow