[Solved] Reordering columns in CSV


So the question is how to reorder your columns in a custom way.

For example you have the following DF and you want to reorder your columns in the following way (indices):

5, 3, rest…

DF

In [82]: df
Out[82]:
   A  B    C  D  E  F  G
0  1  0  0.5  5  1  7  6
1  2  0  0.6  4  0  7  6
2  3  0  0.7  3  1  7  6
3  4  0  0.8  2  0  7  6

columns

In [83]: cols = df.columns.tolist()

In [84]: cols
Out[84]: ['A', 'B', 'C', 'D', 'E', 'F', 'G']

reordered:

In [88]: cols = [cols.pop(5)] + [cols.pop(3)] + cols

In [89]: cols
Out[89]: ['F', 'D', 'A', 'B', 'C', 'E', 'G']

In [90]: df[cols]
Out[90]:
   F  D  A  B    C  E  G
0  7  5  1  0  0.5  1  6
1  7  4  2  0  0.6  0  6
2  7  3  3  0  0.7  1  6
3  7  2  4  0  0.8  0  6

1

solved Reordering columns in CSV