1

I'm loading quite big excel sheets to dataframe and I need to rearrange columns in some elegant way so my coworker could use this data in her SPSS script which is already written.

I know that it's as easy as passing list of column indexes, but my Dataframe has 115 data columns so it's a long list. That's why I'm looking for a elegant way to rearrange columns as follows (zero indexed):

columns 0 to 8, columns 10 to 112, column 9, column 113 to 114

1 Answer 1

2

You can do this:

columns = list(df.columns)
df = df[columns[:9]+columns[10:113]+[columns[9]]+columns[113:]]

Note that, to go from 10 -> 112, you need columns[10:113] because the slicer doesn't consider the last element as part of the interval

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You, I thought it will be something like this :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.