Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sort columns names only not rows in python

Hi i have df_input which needs to be sorted for only column names not by rows(restructuring the dataframe)

df_input.columns

    Out[143]: Index(['product_name', 'price', 'make', 'v_d1', 'v_d4', 'v_d2', 'v_d3'], dtype='object')

My required output column names should be sorted after N columns(here after 3 columns)

df_out.columns
Out[144]: Index(['product_name', 'price', 'make', 'v_d1', 'v_d2', 'v_d3', 'v_d4'], dtype='object')

My input dataframe is as follows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

data = {'product_name': ['laptop', 'printer', 'tablet', 'desktop', 'chair'],
        'price': [1200, 150, 300, 450, 200],
        'make':['Dell','hp','Lenove','iPhone','xyz'],
        'v_d1':[2,44,55,2,1],
        'v_d4':[66,12,55,7,89],
        'v_d2':[54,12,45,77,23],
        'v_d3':[88,69,37,15,10]
        }

df_input = pd.DataFrame(data)
print (df)

Required output dataframe:

data = {'product_name': ['laptop', 'printer', 'tablet', 'desktop', 'chair'],
        'price': [1200, 150, 300, 450, 200],
        'make':['Dell','hp','Lenove','iPhone','xyz'],
        'v_d1':[2,44,55,2,1],
        'v_d2':[54,12,45,77,23],
        'v_d3':[88,69,37,15,10],
        'v_d4':[66,12,55,7,89]
        } 

df_out = pd.DataFrame(data) 

Thanks in advance

>Solution :

If values of columns names are from 0 to 9 is possible use sorted columns with slicing:

df = df[df.columns[:3].tolist() + sorted(df.columns[3:])]
print (df)
  product_name  price    make  v_d1  v_d2  v_d3  v_d4
0       laptop   1200    Dell     2    54    88    66
1      printer    150      hp    44    12    69    12
2       tablet    300  Lenove    55    45    37    55
3      desktop    450  iPhone     2    77    15     7
4        chair    200     xyz     1    23    10    89

More general solution with natural sorting:

from natsort import natsorted
    
data = {'product_name': ['laptop', 'printer', 'tablet', 'desktop', 'chair'],
        'price': [1200, 150, 300, 450, 200],
        'make':['Dell','hp','Lenove','iPhone','xyz'],
        'v_d1':[2,44,55,2,1],
        'v_d4':[66,12,55,7,89],
        'v_d10':[54,12,45,77,23],
        'v_d20':[88,69,37,15,10]
        }

df = pd.DataFrame(data)

df = df[df.columns[:3].tolist() + natsorted(df.columns[3:])]
print (df)
  product_name  price    make  v_d1  v_d4  v_d10  v_d20
0       laptop   1200    Dell     2    66     54     88
1      printer    150      hp    44    12     12     69
2       tablet    300  Lenove    55    55     45     37
3      desktop    450  iPhone     2     7     77     15
4        chair    200     xyz     1    89     23     10
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading