Python Forum
How to sort rows based on specific order
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to sort rows based on specific order
#1
Hi,
I have below dataframe:
I want to arrange (based on column "Name") in the order [A, B,C,D] and by time

Ex:

A
B
C
D
A
B
C
D
A
B
C
D
Name    Time                 sequence   Rank
A       2020/07/20 20:13:20  W1         5 
C       2020/07/20 20:15:20  W1         5
D       2020/07/20 20:19:08  W1         1
B       2020/07/20 20:13:34  W1         5
A       2020/07/20 21:10:40  W2         11 
D       2020/07/20 21:15:20  W2         56
C       2020/07/20 21:19:35  W2         3
B       2020/07/20 21:15:52  W2         4
C       2020/07/20 22:10:40  W3         11 
B       2020/07/20 22:15:20  W3         56
A       2020/07/20 23:19:35  W3         3
D       2020/07/20 23:15:52  W3         4
Desiredoutput
A       2020/07/20 20:13:20  W1         5
B       2020/07/20 20:13:34  W1         5
C       2020/07/20 20:15:20  W1         5
D       2020/07/20 20:19:08  W1         1
A       2020/07/20 21:10:40  W2         11
B       2020/07/20 21:15:52  W2         4
C       2020/07/20 21:19:35  W2         3
D       2020/07/20 21:15:20  W2         56
A       2020/07/20 23:19:35  W3         3
B       2020/07/20 22:15:20  W3         56
C       2020/07/20 22:10:40  W3         11 
D       2020/07/20 23:15:52  W3         4
I searched but I could not find any similar hint(I can find sorting list only)
Reply
#2
In your example they're sorted first by sequence number, then by Name. Is that always the case? If so, make your sort key a tuple with the sequence number first, then the name and you'll get that output.
Reply
#3
SOrt by "Name" column. then by time
Reply
#4
I'm not sure how that would work. In the desired output show, neither the name nor the time are in sorted order. So sorting directly by time won't generate it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Color a table cell based on specific text Creepy 11 2,013 Jul-27-2023, 02:48 PM
Last Post: deanhystad
  Reading Specific Rows In a CSV File finndude 3 1,002 Dec-13-2022, 03:19 PM
Last Post: finndude
  Deleting rows based on cell value in Excel azizrasul 11 2,679 Oct-19-2022, 02:38 AM
Last Post: azizrasul
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 849 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  Pymysql delete specific rows in tableview stsxbel 2 1,095 Aug-18-2022, 09:50 AM
Last Post: ibreeden
  Trying to delete rows above a specific datetime value cubangt 19 11,540 May-09-2022, 08:57 PM
Last Post: deanhystad
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,328 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Extracting Specific Lines from text file based on content. jokerfmj 8 3,044 Mar-28-2022, 03:38 PM
Last Post: snippsat
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,636 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Calculate next rows based on previous values of array divon 0 1,792 Nov-23-2021, 04:44 AM
Last Post: divon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020