Home > Enterprise >  sort columns names only not rows in python
sort columns names only not rows in python

Time:12-25

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:

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

CodePudding user response:

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
  • Related