• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / pandas Absolute Value – Get Absolute Values in a Series or DataFrame

pandas Absolute Value – Get Absolute Values in a Series or DataFrame

December 20, 2021 Leave a Comment

To find the absolute value in pandas, the easiest way is to use the pandas abs() function.

df["Column"] = df["Column"].abs()

You can also use the numpy abs() function and apply it to a column.

df["Column"] = df["Column"].apply(np.abs)

Finding the absolute value of numbers in a column, or the absolute value of all numbers in a DataFrame using pandas is easy. We can use the pandas abs() function to find the absolute values in a column of numbers, or a DataFrame.

Let’s say we have the following DataFrame.

df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
                   'Weight Change': [-16.20, 12.81, -20.45, 15.35, -12.43, -18.52]})

print(df)
# Output: 
    Name  Weight Change
0    Jim         -16.20
1  Sally          12.81
2    Bob         -20.45
3    Sue          15.35
4   Jill         -12.43
5  Larry         -18.52

To get the absolute values of the numbers in the column “Weight Change”, we can use the pandas abs() function in the following Python code:

df["Weight Change"] = df["Weight Change"].abs()

print(df)

# Output:
    Name  Weight Change
0    Jim          16.20
1  Sally          12.81
2    Bob          20.45
3    Sue          15.35
4   Jill          12.43
5  Larry          18.52

As you can see above, all of the numbers are now positive.

Please note, you can use the pandas abs() function on an entire DataFrame if the DataFrame only contains numbers. If we call it on the DataFrame from above, we will receive an error because the “Name” column is made up of strings.

Using numpy abs to Calculate Absolute Values with pandas DataFrame

We can also use the numpy abs() function to calculate the absolute values of the numbers in a column in a pandas DataFrame.

To get the absolute values of the numbers in the column “Weight Change”, we can use the numpy abs() function in the following Python code:

df["Weight Change"] = df["Weight Change"].apply(np.abs)

print(df)

# Output:
    Name  Weight Change
0    Jim          16.20
1  Sally          12.81
2    Bob          20.45
3    Sue          15.35
4   Jill          12.43
5  Larry          18.52

As you can see above, all of the numbers are now positive.

One last thing to note, if you are looking to find the absolute value of a number in regular Python, you can use the Math.fabs() function.

Hopefully this article has been helpful for you to understand how to find the absolute value of numbers in a Series or DataFrame in pandas.

Other Articles You'll Also Like:

  • 1.  Python Destroy Object – How to Delete Objects with del Keyword
  • 2.  How to Check if String Contains Lowercase Letters in Python
  • 3.  Get Difference Between datetime Variables in Python
  • 4.  Get Current Datetime in Python
  • 5.  Using Python to Calculate Average of List of Numbers
  • 6.  Python divmod() Function – Get Quotient and Remainder After Division
  • 7.  Create List of Odd Numbers in Range with Python
  • 8.  Creating a List of Zeros in Python
  • 9.  Python Square Root – Finding Square Roots Using math.sqrt() Function
  • 10.  Union of Lists in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy