【问题标题】:Sorting pandas dataframe according to first column根据第一列对熊猫数据框进行排序
【发布时间】:2015-11-06 19:52:56
【问题描述】:

我正在尝试根据“ID 名称”对该数据框进行排序。如何按字母顺序获取“ID 名称”值(同时保留值的相应时间戳和 cmets)?

          ID Name             Time Stamp         Comments
0          W10D12    2015-01-24 16:40:34     RandComment1 
1           W8D13    2015-01-25 11:51:21     RandComment1   
2           W8D15    2015-01-25 11:51:41     RandComment1  
3           W8D22    2015-01-25 11:51:47     RandComment2 
4           W8D23    2015-01-25 11:51:54     RandComment2
5           W8D25    2015-01-25 11:51:59     RandComment2
6           W2D27    2015-01-25 11:52:03     RandComment2
7          W16D12    2015-01-24 16:41:45     RandComment3
8          W10D13    2015-01-25 11:53:06     RandComment4
9           W8D15    2015-01-25 11:53:27     RandComment4
..            ...                    ...              ...

【问题讨论】:

    标签: python sorting pandas alphabetical columnsorting


    【解决方案1】:

    df.sort('ID Name') 将根据 'ID Name' 按行对 DataFrame 进行排序

    您可以添加一个作为排序 ID 的新列:

    new_col = df['ID Name'].copy()
    new_col.sort()
    df['sorted ID'] = new_col
    

    【讨论】:

      【解决方案2】:

      您可以使用数据框sort 功能,其余列将随之而来:

      result = df.sort_index(by=['ID Name'], ascending=[True])
      

      【讨论】:

      • .sort_values(.sort_values 已弃用)
      猜你喜欢
      • 2014-12-29
      • 2021-10-07
      • 2013-12-15
      • 1970-01-01
      • 2012-06-19
      • 2022-07-19
      • 1970-01-01
      • 2019-04-12
      • 2022-11-02
      相关资源
      最近更新 更多