Search code examples
pythonpython-3.xpandascolumnsorting

Reordering column index of a dataframe


I have a data frame of 15k columns. These columns are to be re-ordered. Such that. These columns are arranged as follows:

1) The first column should have values of third 2) The second column should have values of first 3) The third column should have values of first

Existing Column

0 1 2 3 4 5 6 7 8 .....14998 14999 15000

Desired Columns

2 0 1 5 3 4 8 6 7 .... 15000 14998 14999


Solution

  • IIUC, even simpler solution:

    import itertools
    
    k = [2, -1, -1] * (len(x)//3)
    indexes = np.arange(15000) + np.array(k)
    
    df.iloc[:, indexes]