I have pandas dataframe as below:
import pandas as pd
import numpy as np
df = pd.DataFrame({'CATEGORY': [1, 1, 2, 2],
                    'GROUP': ['A', 'A', 'B', 'B'],
                     'XYZ': [3000, 2500, 3000, 3000],
                  'VAL': [3000, 2500, 3000, 3000],
                  'A_CLASS': [3000, 2500, 3000, 3000],
                  'B_CAL': [3000, 4500, 3000, 1000],
                  'C_CLASS': [3000, 2500, 3000, 3000],
                  'A_CAL': [3000, 2500, 3000, 3000],
                  'B_CLASS': [3000, 4500, 3000, 500],
                  'C_CAL': [3000, 2500, 3000, 3000],
                  'ABC': [3000, 2500, 3000, 3000]})
df
CATEGORY   GROUP   XYZ   VAL    A_CLASS  B_CAL  C_CLASS   A_CAL   B_CLASS   C_CAL  ABC  
1          A       3000   1     3000     3000     3000     3000    3000     3000   3000
1          A       2500   2     2500     4500     2500     2500    4500     2500   2500
2          B       3000   4     3000     3000     3000     3000    3000     3000   3000
2          B       3000   1     3000     1000     3000     3000    500      3000   3000
I want columns in below order in my final dataframe
GROUP, CATEGORY, all columns with suffix "_CAL", all columns with suffix "_CLASS", all other fields
My expected output:
GROUP    CATEGORY   B_CAL    A_CAL   C_CAL   A_CLASS   C_CLASS    B_CLASS   XYZ   VAL   ABC 
A        1          3000     3000    3000    3000      3000       3000      3000   1    3000
A        1          4500     2500    2500    2500      2500       4500      2500   2    2500
A        1          8000     7000    8000    8000      8000       8000      8000   5    8000
B        2          3000     3000    3000    3000      3000       3000      3000   4    3000
B        2          1000     3000    3000    3000      3000       500       3000   1    3000
    
cols = ['GORUP', 'CATEGORY', 'OTHER_COLUMNS']thennewdf = df[cols]df.reindex(cols_sorted, axis=1)is also an option, see the answers for whatcols_sortedshould be. (I think it might be preferred overdf[cols_sorted], see doc).