Pandas abs()

The abs() method in Pandas is used to compute the absolute value of each element in a DataFrame.

Example

import pandas as pd

# create a DataFrame
df = pd.DataFrame({
    'A': [-1, -2, -3],
    'B': [-5, -6, 7],
    'C': [9, -10, 11]
})

# calculate the absolute values absolute_df = df.abs()
print(absolute_df) ''' Output A B C 0 1 5 9 1 2 6 10 2 3 7 11 '''

abs() Syntax

The syntax of the abs() method in Pandas is:

df.abs()

abs() Argument

The abs() method in Pandas takes no arguments.


abs() Return Value

The abs() method will return a new DataFrame with the absolute value of each element.


Example1: Calculate Absolute Values Using abs()

import pandas as pd

# create a DataFrame
df = pd.DataFrame({
    'X': [10, -20, -30, 40],
    'Y': [-50, 60, -70, 80],
    'Z': [-90, 100, 110, -120]
})

# calculate the absolute values absolute_df = df.abs()
print(absolute_df)

Output

   X   Y    Z
0  10  50   90
1  20  60  100
2  30  70  110
3  40  80  120

Here, we have used the abs() method to compute the absolute values of each element in the df DataFrame.

The absolute value of 10 is 10, -20 is 20, -30 is 30 and so on.


Example 2: Working With Complex Numbers

import pandas as pd

# create a DataFrame with complex numbers
df = pd.DataFrame({
    'Complex1': [1+2j, -2-3j, 3+4j],
    'Complex2': [-4+5j, 5-6j, -6-7j]
})

# calculate absolute values of df DataFrame result = df.abs()
print(result)

Output

   Complex1  Complex2
0  2.236068  6.403124
1  3.605551  7.810250
2  5.000000  9.219544

In the above example, the abs() method is applied to the df DataFrame, and it returns the result containing the magnitudes of the complex numbers.

The magnitudes are calculated as the absolute values of the complex numbers using the formula:

√(a^2 + b^2)

Here, a and b are the real and imaginary parts of the complex number, respectively.


Example 3: Absolute Value for DataFrame With Mixed Numeric Data Types

import pandas as pd

# create a DataFrame with different numeric types
df = pd.DataFrame({
    'Integers': [1, -2, -3, 4],
    'Floats': [-1.5, 2.3, -3.7, 4.8],
    'Complex': [1+1j, -1-1j, 1-1j, -1+1j]
})

# apply abs() to get the absolute values or magnitudes absolute_df = df.abs()
print(absolute_df)

Output

   Integers  Floats   Complex
0         1     1.5  1.414214
1         2     2.3  1.414214
2         3     3.7  1.414214
3         4     4.8  1.414214

In this example, we have created the df DataFrame with three columns containing different types of numeric values: integers, floating-point numbers, and complex numbers.

The abs() calculates the absolute value for the integers and floating-point numbers, and the magnitude for the complex numbers, producing a new DataFrame with all non-negative values.


Example 4: Apply abs() to a Specific Column

import pandas as pd

# create a DataFrame
df = pd.DataFrame({
    'A': [-1, 2, -3, 4],
    'B': [5, -6, 7, -8]
})

# apply abs() to a single column df['A'] = df['A'].abs()
print(df)

Output

   A  B
0  1  5
1  2 -6
2  3  7
3  4 -8

Here, we have applied abs() to column A to replace it with its absolute values, and printed the modified DataFrame with column B remaining unchanged.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges