 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to sort multiple columns of a Pandas DataFrame?
To sort multiple columns of a Pandas DataFrame, we can use the sort_values() method.
Steps
- Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df. 
- Print the input DataFrame, df. 
- Initialize a variable col to sort the column. 
- Print the sorted DataFrame. 
Example
import pandas as pd df = pd.DataFrame(    {       "x": [5, 2, 7, 0],       "y": [4, 7, 5, 1],       "z": [9, 3, 5, 1]    } ) print "Input DataFrame is:
", df col = ["x", "y"] df = df.sort_values(col, ascending=[False, True]) print "After sorting column ", col, "DataFrame is:
", df Output
Input DataFrame is: x y z 0 5 4 9 1 2 7 3 2 7 5 5 3 0 1 1 After sorting column ['x', 'y'] DataFrame is: x y z 2 7 5 5 0 5 4 9 1 2 7 3 3 0 1 1
Advertisements
 