How to rearrange Pandas column sequence?

后端 未结 6 503
暗喜
暗喜 2020-12-08 05:16
>>> df =DataFrame({\'a\':[1,2,3,4],\'b\':[2,4,6,8]})
>>> df[\'x\']=df.a + df.b
>>> df[\'y\']=df.a - df.b
>>> df
   a  b   x  y
0          


        
相关标签:
6条回答
  • 2020-12-08 05:22

    You can do the following:

    df =DataFrame({'a':[1,2,3,4],'b':[2,4,6,8]})
    
    df['x']=df.a + df.b
    df['y']=df.a - df.b
    

    create column title whatever order you want in this way:

    column_titles = ['x','y','a','b']
    
    df.reindex(columns=column_titles)
    

    This will give you desired output

    0 讨论(0)
  • 2020-12-08 05:23
    def _col_seq_set(df, col_list, seq_list):
        ''' set dataframe 'df' col_list's sequence by seq_list '''
        col_not_in_col_list = [x for x in list(df.columns) if x not in col_list]
        for i in range(len(col_list)):
            col_not_in_col_list.insert(seq_list[i], col_list[i])
    
        return df[col_not_in_col_list]
    DataFrame.col_seq_set = _col_seq_set
    
    0 讨论(0)
  • 2020-12-08 05:33

    I would suggest you just write a function to do what you're saying probably using drop (to delete columns) and insert to insert columns at a position. There isn't an existing API function to do what you're describing.

    0 讨论(0)
  • 2020-12-08 05:44

    Feel free to disregard this solution as subtracting a list from an Index does not preserve the order of the original Index, if that's important.

    In [61]: df.reindex(columns=pd.Index(['x', 'y']).append(df.columns - ['x', 'y']))
    Out[61]: 
        x  y  a  b
    0   3 -1  1  2
    1   6 -2  2  4
    2   9 -3  3  6
    3  12 -4  4  8
    
    0 讨论(0)
  • 2020-12-08 05:45

    There may be an elegant built-in function (but I haven't found it yet). You could write one:

    # reorder columns
    def set_column_sequence(dataframe, seq, front=True):
        '''Takes a dataframe and a subsequence of its columns,
           returns dataframe with seq as first columns if "front" is True,
           and seq as last columns if "front" is False.
        '''
        cols = seq[:] # copy so we don't mutate seq
        for x in dataframe.columns:
            if x not in cols:
                if front: #we want "seq" to be in the front
                    #so append current column to the end of the list
                    cols.append(x)
                else:
                    #we want "seq" to be last, so insert this
                    #column in the front of the new column list
                    #"cols" we are building:
                    cols.insert(0, x)
    return dataframe[cols]
    

    For your example: set_column_sequence(df, ['x','y']) would return the desired output.

    If you want the seq at the end of the DataFrame instead simply pass in "front=False".

    0 讨论(0)
  • 2020-12-08 05:47

    You could also do something like this:

    df = df[['x', 'y', 'a', 'b']]
    

    You can get the list of columns with:

    cols = list(df.columns.values)
    

    The output will produce something like this:

    ['a', 'b', 'x', 'y']
    

    ...which is then easy to rearrange manually before dropping it into the first function

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