'is there any way to convert the columns in Pandas Dataframe using its mirror image Dataframe structure

the df I have is :

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

I wanted to obtain a Dataframe with columns reversed/mirror image :

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

Is there any way to do that



Solution 1:[1]

You can check

df[:] = df.iloc[:,::-1]
df
Out[959]: 
   0  1  2
0  0  0  0
1  1  0  0
2  0  1  0
3  1  1  0
4  0  0  1
5  1  0  1
6  0  1  1
7  1  1  1

Solution 2:[2]

Here is a bit more verbose, but likely more efficient solution as it doesn't require to rewrite the data. It only renames and reorders the columns:

cols = df.columns
df.columns = df.columns[::-1]
df = df.loc[:,cols]

Or shorter variant:

df = df.iloc[:,::-1].set_axis(df.columns, axis=1)

Output:

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

Solution 3:[3]

There are other ways, but here's one solution:

df[df.columns] = df[reversed(df.columns)]

Output:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BENY
Solution 2 mozway
Solution 3 ddejohn