0

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?

2 Answers 2

2

IIUC,

df = pd.DataFrame(np.random.randint(0,100,(10,6)), columns=[*'ABWXYZ'])

orderedCols = ['Z','Y','W']
nonOrderedCols = [i for i in df.columns if i not in orderedCols]

nonOrderedCols
# ['A', 'B', 'X']

orderedCols
# ['Z', 'Y', 'W']

df.reindex(orderedCols + nonOrderedCols, axis=1)

Output:

    Z   Y   W   A   B   X
0  29  61  65  17  95  59
1  87   2  98   4  79  85
2  53  45   2  77  49  81
3  72  47  58  53  22  24
4  74  66  50  93  29  71
5  23  94  70  38  11  94
6  54  60   7  29   3  33
7  31   1  67  80  68  57
8  99  50  79  28  49  52
9  73  46  77  17  47  93

Note: You can also use nonOrderedCols = df.columns[~df.columns.isin(orderedCols)]

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

1 Comment

This looks promising but the 'or' in your post is confusing.
2

If I understand your question correctly, you would use

 df.sort_values(by=['in_Code','in_Name','Code'])

1 Comment

This didn't work for me. I get an error 'Check for Duplicates'. Is this sorting the values in the three columns, rather than the columns themselves? I would like the columns sorted, don't really care about the values.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.