Reverse the order of columns without changing the column labels pandas dataframe

Question:

I need to reverse the order of my pandas dataframe. But using the following code:

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
df = df.iloc[:, ::-1]

also reverses the order of the column labels. How can i reverse only the data and maintain the column labels? I expect to get:

   A  B  C
0  7  4  1
1  8  5  2
2  9  6  3
Asked By: Murilo

||

Answers:

To modify in place, use:

df[:] = df.iloc[:, ::-1].to_numpy()

Or, for a new object:

out = df.iloc[:, ::-1].set_axis(df.columns, axis=1)
Answered By: mozway

If large DataFrame and performance is important assign to new Dataframe by constructor:

df = pd.DataFrame(np.random.randint(10, size=(1000,1000))).add_prefix('col')
# print (df)
   

In [108]: %timeit df[:] = df.values[:, ::-1]
5.19 ms ± 555 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [109]: %timeit df[:] = df.iloc[:, ::-1].to_numpy()
5.34 ms ± 288 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [110]: %timeit df.iloc[:, ::-1].set_axis(df.columns, axis=1)
2.42 ms ± 357 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [111]: %timeit pd.DataFrame(df.iloc[:, ::-1].to_numpy(), index=df.index,columns=df.columns)
102 µs ± 3.45 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [112]: %timeit pd.DataFrame(df.to_numpy()[:, ::-1], index=df.index, columns=df.columns)
43.7 µs ± 240 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Answered By: jezrael

With shortest assignment:

df[:] = df.values[:, ::-1]

   A  B  C
0  7  4  1
1  8  5  2
2  9  6  3
Answered By: RomanPerekhrest
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.