【问题标题】:Sorting rows in the following unique manner (values for columns can be interchanged within the same row, to sort the row)以以下唯一方式对行进行排序(列的值可以在同一行内互换,以对行进行排序)
【发布时间】:2020-05-06 20:37:41
【问题描述】:

输入数据框:

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

输出数据框应该是这样的: 索引

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

行必须按升序或降序排序,独立于列,表示列的值可以在同一行内互换,以对行进行排序。以以下独特的方式对行进行排序。 请帮忙,我正在处理一件非常重要的事情。

【问题讨论】:

    标签: pandas dataframe pandas-groupby


    【解决方案1】:

    将DataFrame转换为numpy数组并通过np.sortaxis=1排序,然后通过构造函数创建DataFrame

    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
    

    【讨论】:

      猜你喜欢
      • 2018-04-20
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多