Search code examples
pythonpandasdataframereverseflip

Flip DataFrame column order by keeping the Index


I have a similar question to this one: Reverse DataFrame Column, But Maintain the Index

Reversing the rows works fine:

import pandas as pd
df = pd.DataFrame(data=[[1,2,3],[4,5,6],[7,8,9]])
df.iloc[:] = df.iloc[::-1].values

How can I reverse the columns to get this result

   0  1  2
0  3  2  1
1  6  5  4
2  9  8  7

Solution

  • You can use numpy flip to reverse the columns :

    pd.DataFrame(np.flip(df.to_numpy()))
    
        0   1   2
    0   3   2   1
    1   6   5   4
    2   9   8   7