- 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 a column of a Pandas DataFrame?
To sort a column in a Pandas DataFrame, we can use the sort_values() method.
Steps
Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.
Print 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, 10, 5, 1], `"z": [9, 3, 5, 1] } ) print "Input DataFrame is:
", df col = "x" df = df[col].sort_values(ascending=False) print "After sorting column ", col, "DataFrame is:
", df
Output
Input DataFrame is: x y z 0 5 4 9 1 2 10 3 2 7 5 5 3 0 1 1 After sorting column x DataFrame is: 2 7 0 5 1 2 3 0 Name: x, dtype: int64
- Related Articles
- How to sort multiple columns of a Pandas DataFrame?
- How to delete a column from a Pandas DataFrame?
- How to shift a column in a Pandas DataFrame?
- How to delete a column from Pandas DataFrame
- Python - How to select a column from a Pandas DataFrame
- How to rename column names in a Pandas DataFrame?
- Apply uppercase to a column in Pandas dataframe
- Python - Add a zero column to Pandas DataFrame
- Pandas dataframe capitalize first letter of a column
- How to get the list of column headers from a Pandas DataFrame?
- How to replace NaN values by Zeroes in a column of a Pandas DataFrame?
- Merge Pandas DataFrame with a common column
- How to get the sum of a specific column of a dataframe in Pandas Python?
- Capitalize first letter of a column in Pandas dataframe
- Python – Center align column headers of a Pandas DataFrame

Advertisements