I have a pandas dataframe and only want to sort the first three columns in my dataframe in a specific order. The order of the rest of the columns does not matter. I have 40 columns in total.
This thread had a solution but it doesn't seem to work for me: how to sort only some of the columns in a data frame in pandas?
The solution in the link above recommends tackling this problem in three steps by using reindex in the following way:
preordered = list('xyz')
new_order = preordered + list(df.columns - preordered)
df.reindex(columns=new_order)
In my case, the name of the three columns I want as first, second and third column are the following: in_Code, in_Name, and Code - in that specific order.
Since list() can only take on one argument, I used preordered=list(['in_Code','in_Name','Code']) for the first step.
Yet the second step, new_order=preordered + list(df.columns - preordered) gives me a TypeError: cannot perform __sub__ with this index type: Index.
Any help?