- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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
- Related Articles
- Select multiple columns in a Pandas DataFrame
- Python - Select multiple columns from a Pandas dataframe
- Plot multiple columns of Pandas DataFrame using Seaborn
- Python Pandas - Plot multiple data columns in a DataFrame?
- How to sort a column of a Pandas DataFrame?
- Plot multiple columns of Pandas dataframe on the bar chart in Matplotlib
- How to change the order of Pandas DataFrame columns?
- Python Pandas - How to select multiple rows from a DataFrame
- Python Pandas - Query the columns of a DataFrame
- How to sort multiple columns with a single query?\n
- How to select all columns except one in a Pandas DataFrame?
- Python - Renaming the columns of Pandas DataFrame
- How to Merge multiple CSV Files into a single Pandas dataframe ?
- How to select multiple DataFrame columns using regexp and datatypes
- How to Sort CSV by multiple columns in Python ?

Advertisements