Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Pandas DataFrame abs() Method

In this tutorial, we will learn the pandas DataFrame.abs() method. It is an in-built of the pandas module and returns a DataFrame with the absolute numeric value of each element.

This function only applicable when all elements are numeric. The absolute is also called the modulus. To understand the abs() the method we will solve examples and get the absolute value of the DataFrame and for the complex numbers, the absolute value will be a2+b2. The below shows the syntax of DataFrame.abs() method.

Syntax

The syntax required to use this function is as follows:

abs(x)

Here, parameter x can be any number and that can be a positive or negative zero. This function will return a positive zero.

Example: Getting the absolute value of the DataFrame

Let's have a look at the basic example of this function and the code snippet for the same is as follows.

import pandas as pd
df = pd.DataFrame([[23, -85, -0.25],[2, -1.259, -85]],columns=['A', 'B','C'])
print("-----DataFrame-----")
print(df)
print(abs(df))

Once we run the program we will get the following output.


-----DataFrame-----
A B C
0 23 -85.000 -0.25
1 2 -1.259 -85.00
A B C
0 23.0 85.000 0.25
1 2.0 1.259 85.00

Example: TypeError while getting the absolute value of the string

We will get TypeError if try to get the absolute value of string because abs() methods allow only numeric values.

import pandas as pd
df = pd.DataFrame([['abc','xyz','pqr'],[2, -1.259, -85]],columns=['A', 'B','C'])
print("-----DataFrame-----")
print(df)
print(abs(df))

Once we run the program we will get the following output.


-----DataFrame-----
A B C
0 abc xyz pqr
1 2 -1.259 -85
Traceback (most recent call last):
File "<string>", line 8, in <module>
File "/usr/local/lib/python3.8/dist-packages/pandas/core/generic.py", line 1381, in __abs__
return self.abs()
File "/usr/local/lib/python3.8/dist-packages/pandas/core/generic.py", line 9723, in abs
return np.abs(self)
TypeError: bad operand type for abs(): 'str'

Example: Getting the absolute value of DataFrame with complex numbers

In the below example we will get the absolute value series elements with complex numbers. The abs() the method returns only the magnitude part of the number. For complex inputs, the absolute value is ?a2+b2

#importing pandas as pd
import pandas as pd
df = pd.DataFrame([[1.2 + 1j,-22,-12],[2, -1.259, -85]],columns=['A', 'B','C'])
print("-----DataFrame-----")
print(df)
print(abs(df))

Once we run the program we will get the following output.


-----DataFrame-----
A B C
0 1.200000+1.000000j -22.000 -12
1 2.000000+0.000000j -1.259 -85
A B C
0 1.56205 22.000 12.0
1 2.00000 1.259 85.0

Example: Getting the absolute value of a particular column of DataFrame

The below example shows how to get the absolute values of a particular column in a DataFrame.

import pandas as pd
df = pd.DataFrame([[23, -85, -0.25],[2, -1.259, -85],[-0.25,78.65],[2,-4,-4.256]],columns=['A', 'B','C'])
print("-----DataFrame-----")
print(df)
print("---printing absolute value of column 'A' of DataFrame---")
print(df['A'].abs())
print("---printing absolute value of column 'C' of DataFrame---")
print(df['C'].abs())

Once we run the program we will get the following output.


-----DataFrame-----
A B C
0 23.00 -85.000 -0.250
1 2.00 -1.259 -85.000
2 -0.25 78.650 NaN
3 2.00 -4.000 -4.256
---printing absolute value of column 'A' of DataFrame---
0 23.00
1 2.00
2 0.25
3 2.00
Name: A, dtype: float64
---printing absolute value of column 'C' of DataFrame---
0 0.250
1 85.000
2 NaN
3 4.256
Name: C, dtype: float64

Conclusion

In this tutorial, we understand the abs() method of the data frame. We learned the syntax and parameters of DataFrame.abs() method and solve different examples to better understand this topic.