Having this dataframe, what I would is to have the rows that have the start_time with 00:XX:XX at the top (descending for example)
PS: start_time comes from df['start].dt.time
CodePudding user response:
Depending on the exact ordering, use DataFrame.sort_values and possibly Series.str.replace.
00h,01h, ...,23hUse
DataFrame.sort_valueswhich is ascending by default:df = df.sort_values('start_time')23h,22h, ...,00hUse
DataFrame.sort_valueswithascending=False:df = df.sort_values('start_time', ascending=False)00h,23h,22h, ...,01hUse
Series.str.replaceto change00:##:##into24:##:##, thenDataFrame.sort_valueswithascending=False, then change24:##:##back to00:##:##:df['start_time'] = df['start_time'].str.replace(r'^00(:\d{2}:\d{2})', r'24\1', regex=True) df = df.sort_values('start_time', ascending=False) df['start_time'] = df['start_time'].str.replace(r'^24(:\d{2}:\d{2})', r'00\1', regex=True)

