Python

Pandas Series Absolute Value

Python is a high-level programming language that is great for data analysts as Pandas in Python provided an amazing ecosystem of data-centric packages. Pandas in Python provide amazing packages which make data analysis simple and easier.

In Python, the absolute value of a number is almost the same as in math. When you find an absolute value of a number using any Python function, it also returns the nonnegative value of that number. Python programming language provides built-in function and panda packages to find the absolute value of a number or a series. In this article, we are going to discuss this topic in detail.

What is Absolute Value in Python?

If you are new to a series absolute value, let us introduce you to the definition of an absolute value or a series. If you are already familiar with it, let us just give you a recap to remind you of the basic concepts.

Regardless of the sign of a number, the absolute function returns its nonnegative value, and it is also commonly known as a modulus function. The absolute value or modulus of 2 is 2, for example, as is the absolute value or modulus of -2. The absolute() function is commonly used to find the distance from to the positive side of that particular number provided, without considering the sign of the number.

How to Get Absolute Value in the Pandas Series?

As we mentioned earlier, Python provides panda packages to find the absolute value of a number or a series. Pandas packages have a couple of functions to calculate the absolute value and series.abs() is one of those functions. Series.abs() is used to find the absolute value of each number in a dataset.

The abs() is a built-in function of Python available with the standard library. When a number is provided to it, it returns the nonnegative value of that number, without considering the sign of the number. The number could be a complex number, a real number, a floating-point number, or just a simple integer.

However, the magnitude is returned in the case of providing a complex number. Whereas an absolute integer is returned for an integer number, an absolute real number for a real number, and an absolute floating-point number for a floating-point number, regardless of their sign.

What is the Syntax of abs() Method?

The syntax of abs() is very simple, it takes only one parameter; the number, of which it has to calculate the absolute and return its absolute value. Here is the syntax of abs():

The ‘number’ is again any real, complex, floating-point, or simple integer number given as input to get the absolute value. Now, let us explore some examples to learn how to use the abs() function.

Example 1:

In this example, we will take 6 numbers; integer, negative integer, real, floating-point, negative floating-point, and complex number to show what different types of absolute values the abs() function will return. See the code for abs(0) function below:

Pinteger = 5

Ninteger = -5

realNum = 2

PFloat = 3.8

NFloat = -3.8

ComplexNum = 5+9j

print("The absolute value of a positive integer number is =", abs(Pinteger))

print("The absolute value of a negative integer number is =", abs(Ninteger))

print("The absolute value of a real number is =", abs(realNum))

print("The absolute value of a positive float number is:", abs(PFloat))

print("The absolute value of a negative float number is:", abs(NFloat))

print("The absolute value of a complex number is:", abs(ComplexNum))

As you can observe, an integer is returned for an integer, a real number is returned for a real number, a floating-point is returned for a floating-point number, and a magnitude is returned for the complex number. However, the absolute number for negative values is also the same.

Example 2:

Now that we know how the abs() function work and how it calculates the absolute value of a number, let us learn how to use the abs() function to find the absolute value of a series. As we have discussed earlier, Pandas in Python provide the Series.abs() function to find the absolute value of a series, so we will be using that in this example.

The difference between the abs() and Series.abs() function is that Series.abs() do not take any parameter and return the absolute value of the complete dataset instead of just a number. First, you need to create a dataset to pass to the Series and then apply the abs() function. The abs() function goes over every number in the dataset and finds its absolute value. See the simple code below to learn the use of the Series.abs() function.

import pandas as pd

list1 = [1, 2, 3, -4, -10.3, 4+6j]

series1 = pd.Series(list1)

print(series1.abs())

It’s worth noting that the list includes both complex and real numbers. Below is the output of the above code:

Now, you can see that the abs() and series.abs() have the same functionality and return the same result.

Example 3:

Passing a complete array to the abs() makes abs() return the absolute value of each number. However, in this example, we will make you learn how you can calculate the absolute value of a specific number or a row in a dataset. The first step is to create a DataFrame, then provide the name of the row of which you want to find the absolute value, and then finally apply the abs() function to find the absolute value of that specific row.

import pandas as pd

df = pd.DataFrame({'Name'['Herry', 'Jenny', 'Peter', 'Mars'],

'Math': [53, 29, -92, -56.3],
'Physics': [-58, -97, 62, 48]})

df['Absolute Math'] = (df.Math).abs()

df

See the output below and observe that abs() only calculated the absolute value of the math row while the other row remained the same.

Conclusion

In this article, we have learned the general concept of absolute value in math and then we have seen how to find the absolute value in Python. Pandas packages for finding the absolute value of a number are available in the Python programming language. The abs() method in a dataframe is used to find the absolute value of a number, a series, or an individual row.