• 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
  • VBA
  • 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.  Read First Line of File Using Python
  • 2.  Skip Numbers in Python Range
  • 3.  Using Python to Print Plus or Minus Sign Symbol
  • 4.  How to Remove NaN from List in Python
  • 5.  How to Check if Number is Power of 2 in Python
  • 6.  Python tan – Find Tangent of Number in Radians Using math.tan()
  • 7.  Length of Set Python – Get Set Length with Python len() Function
  • 8.  pandas min – Find Minimum Value of Series or DataFrame
  • 9.  Create Empty String Variable in Python
  • 10.  pandas nsmallest – Find Smallest Values in Series or Dataframe

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy