簡體   English   中英

根據第一列對熊貓數據框進行排序

[英]Sorting pandas dataframe according to first column

我正在嘗試根據“ ID名稱”對該數據框進行排序。 如何按字母順序獲取“ ID名稱”值(同時保持值的相應時間戳和注釋)?

          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
..            ...                    ...              ...

您可以使用數據框排序功能,其余各列如下:

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

df.sort('ID Name')將基於'ID Name'按行對DataFrame進行排序

您可以添加一個新列,它是已排序的ID:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM