Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I managed to get a solution to sorting values by a particular column in my pandas series. However, the problem is that sorting purely by time doesn't allow me to factor in different dates in which the time occurred. I understand that I could potentially hard code the order and use .loc to apply the order but wanted to find out if there was a simpler solution to sort primarily by week (earliest week first) and by time (0-23hours for each week).

Here is a sample of the dataframe I have again:

weeknum   time_hour

16-22Jun  0.0           5

2-8Jun    0.0           3

23-29Jun  0.0          11

9-15Jun   0.0           3

16-22Jun  1.0           3

2-8Jun    1.0           6

23-29Jun  1.0           3

9-15Jun   1.0           8

16-22Jun  2.0           3

2-8Jun    2.0           6

23-29Jun  2.0           3

16-22Jun  3.0           4

2-8Jun    3.0           2

23-29Jun  3.0           3

9-15Jun   3.0           4

16-22Jun  4.0           2

2-8Jun    4.0           7

23-29Jun  4.0           1

9-15Jun   4.0           7

16-22Jun  5.0           2

2-8Jun    5.0           9

23-29Jun  5.0           9

9-15Jun   5.0          12

16-22Jun  6.0           5

2-8Jun    6.0          12

23-29Jun  6.0           6

9-15Jun   6.0          14

16-22Jun  7.0          12

2-8Jun    7.0          17

23-29Jun  7.0          19

This is my code:

merged_clean.groupby('weeknum')['time_hour'].value_counts().sort_index(level=['time_hour'])

1 Answer

0 votes
by (25.1k points)

Use the sorted function by multiple keys for sorting MultiIndex with convert first number before - and for change order use DataFrame.reindex:

s = merged_clean.groupby('weeknum')['time_hour'].value_counts()

idx = sorted(s.index, key = lambda x: (int(x[0].split('-')[0]), x[1]))

s = s.reindex(idx)

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
0 votes
1 answer

Browse Categories

...