Pandas DataFrame sort_values() Method


Preparation

Before any data manipulation can occur, two (2) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install numpy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import numpy as np 

DataFrame sort_values()

The sort_values() method sorts (re-arranges) the elements of a DataFrame.

The syntax for this method is as follows:

DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last', ignore_index=False, key=None)
ParameterDescription
byThis parameter is a string or a list of strings. These comprise the index levels/columns to sort. Dependent on the selected axis.
axisIf zero (0) or index is selected, apply to each column. Default is 0 (column). If zero (1) or columns, apply to each row.
ascendingBy default, True. Sort is conducted in ascending order. If False, descending order.
inplaceIf False, create a copy of the object. If True, the original object updates. By default, False.
kindAvailable options are quicksort, mergesort, heapsort, or stable. By default, quicksort. See numpy.sort for additional details.
na_positionAvailable options are first and last (default). If the option is first, all NaN values move to the beginning, last to the end.
ignore_indexIf True, the axis numbering is 0, 1, 2, etc. By default, False.
keyThis parameter applies the function to the values before a sort. The data must be in a Series format and applies to each column.

For this example, a comma-delimited CSV file is read in. This DataFrame sorts on the Capital column in descending order.

df = pd.read_csv('countries.csv')
result = df.sort_values(by=['Capital'], ascending=False)
print(result)
  • Line [1] reads in a comma-delimited CSV file and saves to df.
  • Line [2] sorts the DataFrame on the Capital column in descending order. The output saves to result.
  • Line [3] outputs the result to the terminal.

Output

 CountryCapitalPopulationArea
6USA Washington  328239523  9833520
4Poland     Warsaw   38383000   312685
3Italy       Rome   60317116   301338
1France      Paris   67081000   551695
5Russia     Moscow  146748590 17098246
2Spain     Madrid   47431256   498511
8India      Dheli 1352642280  3287263
0GermanyBerlin   83783942   357021
7IndiaBeijing 1400050000  9596961

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.