0

For the following dataframe DF, I want to sort column 't' by the hours between 9:30 and 15:55. Is there a way to use DF.sort_values(by='t', '16:00 < t > 9:30') or something along those lines? Thanks!

        d           t           v       vw          o       c       h       l       n   Unnamed: 9
0       2022-05-13  09:30:00    4113    396.5365    396.80  396.30  396.80  396.30  41  NaN
1       2022-05-13  09:35:00    11846   396.4575    396.37  396.47  396.50  396.25  48  NaN
2       2022-05-13  09:40:00    3910    396.4668    396.35  396.54  396.54  396.30  21  NaN
3       2022-05-13  09:45:00    5138    396.3902    396.55  396.20  396.69  396.20  25  NaN
4       2022-05-13  09:50:00    9944    396.0286    396.23  395.88  396.23  395.88  49  NaN
...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...
19346   2022-10-12  23:40:00    2530    357.4370    357.43  357.44  357.46  357.42  40  NaN
19347   2022-10-12  23:45:00    5554    357.4700    357.44  357.50  357.51  357.44  82  NaN
19348   2022-10-12  23:50:00    13121   357.6356    357.51  357.60  357.71  357.51  210 NaN
19349   2022-10-12  23:55:00    9906    357.5821    357.56  357.59  357.66  357.52  106 NaN
19350   2022-10-13  00:00:00    461     357.5896    357.60  357.58  357.60  357.58  5   NaN

19351 rows × 10 columns
connorc
  • 27
  • 6
  • `sort` or [How do I select rows from a DataFrame based on column values?](https://stackoverflow.com/questions/17071871/how-do-i-select-rows-from-a-dataframe-based-on-column-values) ? – Luuk Oct 15 '22 at 13:59
  • That was helpful, thank you @Luuk. Follow up question... I'm now using DF.loc[(DF['t'] >= 9:30) & (DF['t'] <= 15:55)] but returning 'SyntaxError: invalid syntax' and it is referring to the times (9:30, 15:55). What direction would you point me in to figure this one out? – connorc Oct 15 '22 at 14:22
  • You want to compare `DF['t']` with a timevalue, and `9:30` is not a valid timevalue. You have to use something like: `'09:30'` . More details in this question/answer: [How to compare times of the day?](https://stackoverflow.com/questions/1831410/how-to-compare-times-of-the-day) – Luuk Oct 15 '22 at 14:26

0 Answers0