masterofpuppets
masterofpuppets

Reputation: 139

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

Upvotes: 1

Views: 206

Answers (2)

sammywemmy
sammywemmy

Reputation: 28644

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

Upvotes: 0

BENY
BENY

Reputation: 323226

Pass the reverse to column by add ,

df.iloc[:] = df.iloc[:,::-1].values
df
   0  1  2
0  3  2  1
1  6  5  4
2  9  8  7

Upvotes: 2

Related Questions