Back

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

I would like to sort the values of my pandas series by the second 'column' in my series.

This is code I have:

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

This is a sample of the data I am trying to sort:

weeknum   time_hour

16-22Jun  10.0         50

          11.0         50

          15.0         44

          12.0         41

          14.0         38

          13.0         34

          16.0         33

          8.0          30

          9.0          29

          18.0         28

          20.0         28

          21.0         23

          19.0         20

          17.0         19

          22.0         16

          7.0          12

          23.0          8

          0.0           5

          6.0           5

          3.0           4

          1.0           3

          2.0           3

          4.0           2

          5.0           2

2-8Jun    11.0         66

          12.0         56

          9.0          55

          13.0         53

          10.0         52

          14.0         41

The idea is to sort this such that it is sorted by time_hour. I know I have tried this

merged_clean.groupby('weeknum')['time_hour'].value_counts().sort_values()

but from what I see, this sorts the rows by the values

1 Answer

0 votes
by (25.1k points)

You can use the sort_index method with level set to 'time_hour' column. Like this:

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

Related questions

Browse Categories

...