7. Sort Lexicographically Unordered Columns After Casting to Categorical - Deepstash

7. Sort Lexicographically Unordered Columns After Casting to Categorical

The above sorting using the key parameter can be confusing to some people. Is there a cleaner way? Pandas is arguably the most versatile library for data processing, and you can expect that there is something neat to solve this relatively common problem — converting these lexicographically unordered columns to categorical data.

  • We define a CategoricalDtype by specifying the order of the months.
  • We cast the month column to the new defined category.
  • When we sort the month, it will use the order of the months in the category data definition.

9

13 reads

The idea is part of this collection:

Machine Learning With Google

Learn more about computerscience with this collection

Understanding machine learning models

Improving data analysis and decision-making

How Google uses logic in machine learning

Related collections

Read & Learn

20x Faster

without
deepstash

with
deepstash

with

deepstash

Personalized microlearning

100+ Learning Journeys

Access to 200,000+ ideas

Access to the mobile app

Unlimited idea saving

Unlimited history

Unlimited listening to ideas

Downloading & offline access

Supercharge your mind with one idea per day

Enter your email and spend 1 minute every day to learn something new.

Email

I agree to receive email updates