Home » Pandas: How to Sort Rows by Absolute Value

Pandas: How to Sort Rows by Absolute Value

by Tutor Aspire

You can use the following methods to sort the rows of a pandas DataFrame based on the absolute value of a column:

Method 1: Sort by Absolute Value (smallest abs. value shown first)

df.reindex(df['my_column'].abs().sort_values().index)

Method 2: Sort by Absolute Value (largest abs. value shown first)

df.reindex(df['my_column'].abs().sort_values(ascending=False).index)

The following examples show how to use each method in practice with the following pandas DataFrame that contains information about various basketball players:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'player': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   'over_under': [4, -9, 2, 0, 1, 12, -4, -5]})
                   
#view DataFrame
print(df)

  player  over_under
0      A           4
1      B          -9
2      C           2
3      D           0
4      E           1
5      F          12
6      G          -4
7      H          -5

Example 1: Sort by Absolute Value (smallest abs. value shown first)

We can use the following syntax to sort the rows of the DataFrame based on the absolute value of the over_under column:

#sort DataFrame based on absolute value of over_under column
df_sorted = df.reindex(df['over_under'].abs().sort_values().index)

#view sorted DataFrame
print(df_sorted)

  player  over_under
3      D           0
4      E           1
2      C           2
0      A           4
6      G          -4
7      H          -5
1      B          -9
5      F          12

Notice that the rows are sorted from smallest absolute value in the over_under column to largest absolute value.

Example 2: Sort by Absolute Value (largest abs. value shown first)

We can use the following syntax to sort the rows of the DataFrame based on the absolute value of the over_under column:

#sort DataFrame based on absolute value of over_under column
df_sorted = df.reindex(df['over_under'].abs().sort_values(ascending=False).index)

#view sorted DataFrame
print(df_sorted)

  player  over_under
5      F          12
1      B          -9
7      H          -5
0      A           4
6      G          -4
2      C           2
4      E           1
3      D           0

Notice that the rows are sorted from largest absolute value in the over_under column to smallest absolute value.

Note: You can find the complete documentation for the pandas sort_values() function here.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

Pandas: How to Sort by Date
Pandas: How to Sort Columns by Name
Pandas: How to Sort by Both Index and Column

You may also like