0

i have been trying to reverse the order of the columns I have in my dataframe but all the solutions I tried are not working for some reason.
Not sure what is the bug here, I'm pretty inexperienced with python, can someone tell me where I am going wrong with this?

I referred to this Reverse DataFrame column order

And these are the options I have tried:

import os
import pandas as pd
import dask
import dask.dataframe as dd
df3 = pd.read_csv(cwd + "/filepathA/filetoReaddata.csv", sep=" ", header =  None)

df3.columns = ["x_coord", "y_coord"]

#(option 1) df3.iloc[:, ::-1]
#(option 2) reversed(df3.columns)
#(option 3) df3.columns[::-1]
df3.head()    

And they all return me the same dataframe with no changes made to the order. I.E it's the same as when I first read it in.

This is what my dataframe looks like so I want it to be y_coord x_coord instead, with the values swapped as well.

    x_coord         y_coord
0   0.000000        1.000000
1   38816.039612    34379.960205
2   38806.429627    34385.380188
3   38813.329590    34379.880188
4   38807.119629    34379.730164

Any help is appreciated thanks!!

Megan Darcy
  • 530
  • 5
  • 15

2 Answers2

3

This should do:

df = df.reindex(df.columns[::-1], axis=1)
sarrysyst
  • 217
  • 1
  • 8
1

Probably you can try this,

df[df.columns[::-1]] = df[df.columns]

        x_coord       y_coord
0      1.000000      0.000000
1  34379.960205  38816.039612
2  34385.380188  38806.429627
3  34379.880188  38813.329590
4  34379.730164  38807.119629
sushanth
  • 8,275
  • 3
  • 17
  • 28