Karan_009
Karan_009

Reputation: 81

Sorting rows in the following unique manner (values for columns can be interchanged within the same row, to sort the row)

Input Data frame:

 1.      0th col   1st_col   2nd_col
 2.       23         46         6
 3.       33          56         3
 4.       243        2          21   

The output data frame should be like: Index

 1.    0th col    1st_col     2nd_col
 2.      6          23          46  
 3.      3          33          56
 4.      2          21          243  

The rows have to be sorted in ascending or descending order, Independent of columns, Means values for columns can be interchanged within the same row, to sort the row. Sorting rows in the following unique manner. Please Help, I am in the middle of something very important.

Upvotes: 3

Views: 50

Answers (1)

jezrael
jezrael

Reputation: 863031

Convert DataFrame to numpy array and sort by np.sort with axis=1, then create DataFrame by constructor:

df1 = pd.DataFrame(np.sort(df.to_numpy(), axis=1), 
                   index=df.index, 
                   columns=df.columns)
print (df1)
   0th col  1st_col  2nd_col
1        6       23       46
2        3       33       56
3        2       21      243

Upvotes: 4

Related Questions