11.7 C
London
Sunday, May 19, 2024
HomePandas in PythonGeneral Functions in PythonPandas: How to Sort Rows by Absolute Value

Pandas: How to Sort Rows by Absolute Value

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories