How to use pandas to realize sorting in Python
Python in how to use pandas to achieve sorting, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Pandas sorting
Sorting in pandas is also very simple, and basically consistent with the process principles on Excel, after all, are data tools. As follows:
-DataFrame.sort_values (), you can sort it.
-the first parameter specifies that the sort is based on the key column
-the second parameter specifies the ascending and descending order of the key column corresponding to the first parameter
-you can also set axis=1 to sort by row (less practical application scenarios)
Sorting in Excel can customize the sequence, that is, let the user customize the order, in pandas, this function is not implemented in sort_values.
> in fact, custom sequences are also placed in separate functions in Excel
For example, you want the class to be sorted according to "5, 4, 2, 1, 3, 6, 7", as follows:
-in pandas, you need to construct a column Categorical as an auxiliary column.
The second parameter specifies the order when the Categorical is instantiated
-finally, the custom sorting can be completed by also calling the sort_values method.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.