Sorting based on two fields in python
Python has a stable sort, so provided that performance isn't an issue the simplest way is to sort it by field 2 and then sort it again by field 1. Show That will give you the result you want, the only catch is that if it is a big list (or you want to sort it often) calling sort twice might be an unacceptable overhead.
Doing it this way also makes it easy to handle the situation where you want some of the columns reverse sorted, just include the 'reverse=True' parameter when necessary. Otherwise you can pass multiple parameters to itemgetter or manually build a tuple. That is probably going to be faster, but has the problem that it doesn't generalise well if some of the columns want to be reverse sorted (numeric columns can still be reversed by negating them but that stops the sort being stable). So if you don't need any columns reverse sorted, go for multiple arguments to itemgetter, if you might, and the columns aren't numeric or you want to keep the sort stable go for multiple consecutive sorts. Edit: For the commenters who have problems understanding how this answers the original question, here is an example that shows exactly how the stable nature of the sorting ensures we can do separate sorts on each key and end up with data sorted on multiple criteria:
This is a runnable example, but to save people running it the output is:
Note in particular how in the
second step the You can sort pandas DataFrame by one or multiple (one or more) columns using sort_values() method and by ascending or descending order. To specify the order, you have to use In this article, I will explain how to sort pandas DataFrame with one or multiple columns.
By default sort_values() return a copy DataFrame with the result of the sort. To sort on current DataFrame use If you are in a hurry, below are some quick examples of how to sort by multiple columns in pandas DataFrame.
Let’s create a DataFrame with a few rows and columns and execute some examples to learn how sort works.
Yields below output.
2. Sort Multiple Columns in pandas DataFrameBy using the sort_values() method you can sort multiple columns in DataFrame by ascending or descending order. When not specified order, all columns specified are sorted by ascending order.
Yields below output.
In case if you wanted to update the existing DataFrame use
Yields same output as above. 3. Sort in an Ascending OrderUse
Yields below output.
4. Sort Multiple Columns in Descending OrderIn case you wanted to sort by descending order, use
Yields below output.
ConclusionIn this article, you have learned how to sort a DataFrame by multiple columns using Dataframe.sort_values() by ascending or descending order. Happy Learning !! You May Also Like
Reference
How do I sort by two criteria in Python?Use sorted() and operator. itemgetter() to sort a list by two fields. Call sorted(a_list, key = k) with k as operator. itemgetter(i, j) to sort the list by the i -th element and then by the j -th element.
How do you sort by two columns in Python?You can sort pandas DataFrame by one or multiple (one or more) columns using sort_values() method and by ascending or descending order. To specify the order, you have to use ascending boolean property; False for descending and True for ascending.
How do you sort by two values?Example Problem: Array of arrays (finalArray) with first entry a folder path and second entry the file name; sort so that array is arranged by folder first, and within identical folders, by file name. finalArray. sort((x: any, y: any): number => { const folder1: string = x[0].
How do you sort multiple values in Python?Sort a list of objects by multiple attributes in Python. Using list. sort() function. A Pythonic solution to in-place sort a list of objects using multiple attributes is to use the list. sort() function. ... . Using sorted() function. The list. sort() function modifies the list in-place.. |