6. Sort by Custom Functions - Deepstash

6. Sort by Custom Functions

6. Sort by Custom Functions

Apparently, the sorted data isn’t something that we expect — the months are not in the desired order. To make this happen, we can take advantage of the sort_method taking a key parameter, to which we can pass a custom function for sorting, just like Python’s built-in sorted function. A possible solution is shown below.

  • The key takes a callable, and we use a custom function here. Besides, this parameter is only available with pandas 1.1.0+.
  • Unlike the key parameter used in sorted() , the key function applies to each of the sorting columns in the sort_values method.

5

4 reads

It's time to

READ

LIKE

A PRO!

Jump-start your

reading habits

, gather your

knowledge

,

remember what you read

and stay ahead of the crowd!

It takes just 5 minutes a day.


TRY THE DEEPSTASH APP

+2M Installs

4.7 App Score