Move column by name to front of table in pandas

后端 未结 9 831
你的背包
你的背包 2020-12-04 07:40

Here is my df:

                             Net   Upper   Lower  Mid  Zsore
Answer option                                                
More than once a da         


        
相关标签:
9条回答
  • 2020-12-04 07:56

    Maybe I'm missing something, but a lot of these answers seem overly complicated. You should be able to just set the columns within a single list:

    Column to the front:

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

    Or if instead, you want to move it to the back:

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

    Or if you wanted to move more than one column:

    cols_to_move = ['Mid', 'Zsore']
    df           = df[ cols_to_move + [ col for col in df.columns if col not in cols_to_move ] ]
    
    0 讨论(0)
  • 2020-12-04 07:56

    You can use the df.reindex() function in pandas. df is

                          Net  Upper   Lower  Mid  Zsore
    Answer option                                      
    More than once a day  0%  0.22%  -0.12%    2     65
    Once a day            0%  0.32%  -0.19%    3     45
    Several times a week  2%  2.45%   1.10%    4     78
    Once a week           1%  1.63%  -0.40%    6     65
    

    define an list of column names

    cols = df.columns.tolist()
    cols
    Out[13]: ['Net', 'Upper', 'Lower', 'Mid', 'Zsore']
    

    move the column name to wherever you want

    cols.insert(0, cols.pop(cols.index('Mid')))
    cols
    Out[16]: ['Mid', 'Net', 'Upper', 'Lower', 'Zsore']
    

    then use df.reindex() function to reorder

    df = df.reindex(columns= cols)
    

    out put is: df

                          Mid  Upper   Lower Net  Zsore
    Answer option                                      
    More than once a day    2  0.22%  -0.12%  0%     65
    Once a day              3  0.32%  -0.19%  0%     45
    Several times a week    4  2.45%   1.10%  2%     78
    Once a week             6  1.63%  -0.40%  1%     65
    
    0 讨论(0)
  • 2020-12-04 08:01

    Here is a generic set of code that I frequently use to rearrange the position of columns. You may find it useful.

    cols = df.columns.tolist()
    n = int(cols.index('Mid'))
    cols = [cols[n]] + cols[:n] + cols[n+1:]
    df = df[cols]
    
    0 讨论(0)
  • 2020-12-04 08:03

    The most simplist thing you can try is:

    df=df[[ 'Mid',   'Upper',   'Lower', 'Net'  , 'Zsore']]
    
    0 讨论(0)
  • 2020-12-04 08:07

    I didn't like how I had to explicitly specify all the other column in the other solutions so this worked best for me. Though it might be slow for large dataframes...?

    df = df.set_index('Mid').reset_index()

    0 讨论(0)
  • 2020-12-04 08:07

    To reorder the rows of a DataFrame just use a list as follows.

    df = df[['Mid', 'Net', 'Upper', 'Lower', 'Zsore']]
    

    This makes it very obvious what was done when reading the code later. Also use:

    df.columns
    Out[1]: Index(['Net', 'Upper', 'Lower', 'Mid', 'Zsore'], dtype='object')
    

    Then cut and paste to reorder.


    For a DataFrame with many columns, store the list of columns in a variable and pop the desired column to the front of the list. Here is an example:

    cols = [str(col_name) for col_name in range(1001)]
    data = np.random.rand(10,1001)
    df = pd.DataFrame(data=data, columns=cols)
    
    mv_col = cols.pop(cols.index('77'))
    df = df[[mv_col] + cols]
    

    Now df.columns has.

    Index(['77', '0', '1', '2', '3', '4', '5', '6', '7', '8',
           ...
           '991', '992', '993', '994', '995', '996', '997', '998', '999', '1000'],
          dtype='object', length=1001)
    
    0 讨论(0)
提交回复
热议问题