In this Python tutorial, we will learn how to get the absolute value in Python. First, we will use one of Pythons built-in functions abs() to do this. In this section, we will go through a couple of examples of how to get the absolute value. Second, we will import data with Pandas and use the abs() method to get the absolute values in a dataframe.

Prerequisites

In this post we are going to use the built in function abs() and fabs(). The latter function is part of the math module and, therefore, we don’t need to install anything. However, if you plan on working with Pandas you will have to install this Python package.

Now, before we go on with the examples on how to get the absolute value of a number using Python, we will answer to questions.

What is an absolute value?

Pretty simple; it means how far a value is from zero.

How do I get the absolute value in Python?

  • Save

This is easy, as long as you have an integer or a float: abs(-33) as this function (abs()) returns the absolute value.

Python abs() Function

The Python abs() function is one of the math functions in Python. This function will return the positive absolute value of a specific number or an expression. In the next sections, we will see plenty of examples of how to get the absolute value in Python. First, however, we are going to have a look at the syntax of the abs() function.

Python abs syntax

The syntax of the abs() function is shown below, Here’s how to get the absolute value in Python:

# Get absolute value of x abs(x)
Code language: Python (python)
absolute value in python
  • Save

Now, x can be any number that we want to find the absolute value for. For instance, if x is positive or negative zero, Pythons abs() function will return positive zero.

If we, however, put in something that is not a number we will get a TypeError (“bad operand type for abs(): ‘str’”.

python abs() value typeError
  • Save

How to get Absolute Value in Python with abs() Example 1

The abs function will enable us to find the absolute value of a numeric value. Here’s how to get the absolute value of an integer:

# Print the absolute value of an integer abs(-33)
Code language: Python (python)

As you can see, it is pretty simple to work with the abs() method. That is, you just type the method and put your negative number between the parantheses and press enter.

Python abs() Example 2

Now, if we have a list of numbers, we cannot use the abs() function as we did in the first example. Note, if we do we get a TypeError, again. Thus, in this example, we are going to use Python list comprehension and the abs() function.

# List of negative integers numbers = [-1, -2.1, -3, -444] # Get the absolute values from ints in a list: [abs(number) for number in numbers]
Code language: Python (python)

In the code chunk above, we first created alist with negative numbers. Second, we used Python list comrehension to loop through these numbers and used the abs() function on each separate number.

Use Python’s math (fabs()) to Ge Absolute Value

Note, it is also possible to import the math module and use the fabs() method to get the absolute value of a number in Python. However, when using fabs(), we will get the absolute value as a float:

import math # Get absolute value with fabs() from math: math.fabs(-33)
Code language: Python (python)
math fabs() can be used instead of Python abs() to get te absolute value
  • Save

Note that using math and the fabs() function to get the absolute value results in a float number!

Python get Absolute Values in Python using Pandas

Now, if we want to get absolute values from a dataset we can use Pandas to create a dataframe from a datafile (e.g., CSV). In this Python absolute value example, we are going to find the absolute values for all the records present in a dataframe. First, we will use Python to get absolute values in one column using Pandas abs method. Second, we will do the same but this for two columns in the dataset. Finally, we will get the absolute values for all columns in the Pandas dataframe.

excel file to import using pandas
  • Save

Now, for this absolute value in Python example, we are going to use the CSV data in the image above. If needed, see the post about Pandas read csv method to understand the steps in importing data from a CSV file. Here’s how to do it, with the example file (python_absolute_value.csv):

import pandas as pd # Import data from .csv df = pd.read_csv('python_absolute_value.csv')
Code language: Python (python)

Now, when we have the data loaded we are ready to get the absolute values using Python Pandas.

Python Pandas Absolute Values Example 1

In the first Python absolute values example using Pandas, we are going to select one column (“D”):

# Absolute value from one column df['D'].abs()
Code language: Python (python)
python absolute value
  • Save

Python Pandas Absolute Values Example 2

Now, in the second absolute values in Python example, we are going to select two columns (“D” and “F”):

# Absolute value from two columns df[['D', 'F']].abs()
Code language: Python (python)
python absolute value
  • Save

Now, if needed there’s more information about slicing and indexing Pandas dataframes in that post.

Python Pandas Absolute Values Example 3

Finally, we are going to get the absolute values from all columns in the Pandas dataframe:

df.abs()
Code language: Python (python)
Python absolute value
  • Save

Absolute Value in Python YouTube Tutorial:

Here’s a YouTube tutorial explaining, by examples, how to get the absolute value in Python:

how to get the absolute value in Python

Conclusion

Now, in this post, we learned how to get the absolute value in Python. It was pretty simple, we just used the abs() function. Second, we learned how to do the same task but with data stored on our computers (e.g., from a CSV file).

python absolute value
  • Save
Share via
Copy link
Powered by Social Snap