Sorting Tables for Ordinal Variables

Screen Link: https://app.dataquest.io/m/285/frequency-distributions/4/sorting-tables-for-ordinal-variables

I do not understand how the expected answer is sorted. The instructions say: Order the table by unique values in a descending order (not alphabetically). It does seem to be sorted in any manner.

Hi Rich. They’re ordering by the labels based on their meaning, going from much more than average down to very few points. It’s very similar to the example given in the lesson, but expanded to be more granular:

We want to sort the labels in an ascending or descending order, but using Series.sort_index() doesn’t work because the method can’t infer quantities from words like “few points”. Series.sort_index() can only order the index alphabetically in an ascending or descending order.

I hope that helps clear up some of the confusion.

2 Likes

Thanks April. I think where I am confused is in how you get the indexes for the iloc.

I think what I did for that lesson is I first use .value_counts() to find out the original order, and then base the index for the iloc on that.

So for example, if we look at the example from the lesson, this is what we get from wnba['PTS_ordinal_scale'].value_counts():

a lot of points    79
few points         27
many points        25
very few points    12

Since the result from .value_counts() is a Series, we can say that a lot of points is at index 0, few points is at index 1, etc. Based on this, if I wanted to use iloc to reorder the list from greatest to least based on the label meaning, I could use the order 0, 2, 1, 3 to reference each one. I can change the code to this:

wnba['PTS_ordinal_scale'].value_counts().iloc[[0, 2, 1, 3]]
5 Likes

That makes perfect sense! Thanks so much.

What if there are 200 labels? It would be much more instructional to do the mapping of labels to iloc list by coding, not by manual assignment.

2 Likes

That clear my confusion too. Thank you!

True…

I am thinking of dictionary value sort…we can change outcome of value.counts() to dictionary and then sort value.