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
, ...,23h
Use
DataFrame.sort_values
which is ascending by default:df = df.sort_values('start_time')
23h
,22h
, ...,00h
Use
DataFrame.sort_values
withascending=False
:df = df.sort_values('start_time', ascending=False)
00h
,23h
,22h
, ...,01h
Use
Series.str.replace
to change00:##:##
into24:##:##
, thenDataFrame.sort_values
withascending=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)