Pandas Tutorial Pandas References

Pandas DataFrame - abs() function



The Pandas DataFrame abs() function returns a Series/DataFrame with absolute numeric value of each element. This function only applies to elements that are all numeric.

Syntax

DataFrame.abs()

Parameters

No parameter is required.

Return Value

Returns Series/DataFrame containing the absolute value of each element.

Example: Applying on whole DataFrame

In the example below, a DataFrame df is created. The abs() function is used to get a DataFrame with absolute numeric value of each element.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "a": [5, -4, 2, -8],
  "b": [-10, -20, 2, -5],
  "c": [10, 20, -30, -5]
})

print("The DataFrame is:")
print(df)

#Getting absolute value of whole dataframe
print("\ndf.abs() returns:")
print(df.abs())

The output of the above code will be:

The DataFrame is:
   a   b   c
0  5 -10  10
1 -4 -20  20
2  2   2 -30
3 -8  -5  -5

df1.abs() returns:
   a   b   c
0  5  10  10
1  4  20  20
2  2   2  30
3  8   5   5

Example: Applying on complex element

For complex element, it returns its absolute value. Consider the example below:

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "x": [3+4j, 3-4j, 5],
  "y": [-3+4j, -3-4j, -5],
})

print("The DataFrame is:")
print(df)

#Getting absolute value of all elements
print("\ndf.abs() returns:")
print(df.abs())

The output of the above code will be:

The DataFrame is:
          x         y
0  3.0+4.0j -3.0+4.0j
1  3.0-4.0j -3.0-4.0j
2  5.0+0.0j -5.0+0.0j

df.abs() returns:
     x    y
0  5.0  5.0
1  5.0  5.0
2  5.0  5.0

Example: Applying on selected columns

Instead of whole DataFrame, the abs() function can be applied on selected columns. Consider the example below:

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "a": [5, -4, 2, -8],
  "b": [-10, -20, 2, -5],
  "c": [10, 20, -30, -5]
})

print("The DataFrame is:")
print(df)

#Applying on single column
print("\ndf['a'].abs() returns:")
print(df["a"].abs())

#Applying on multiple columns
print("\ndf[['a', 'c']].abs() returns:")
print(df[["a", "c"]].abs())

The output of the above code will be:

The DataFrame is:
   a   b   c
0  5 -10  10
1 -4 -20  20
2  2   2 -30
3 -8  -5  -5

df['a'].abs() returns:
0    5
1    4
2    2
3    8
Name: a, dtype: int64

df[['a', 'c']].abs() returns:
   a   c
0  5  10
1  4  20
2  2  30
3  8   5

Example: Sorting a DataFrame based on absolute value of a column

Consider one more example where the dataframe df is sorted based on the absolute value of column 'c'.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "a": [5, -4, 2, -8],
  "b": [-10, -20, 2, -5],
  "c": [10, 20, -30, -5]
})

print("The DataFrame is:")
print(df)

#sorting the dataframe based on 
#absolute value of column c
print("\ndf.loc[df['c'].abs().argsort()] returns:")
print(df.loc[df["c"].abs().argsort()])

The output of the above code will be:

The DataFrame is:
   a   b   c
0  5 -10  10
1 -4 -20  20
2  2   2 -30
3 -8  -5  -5

df.loc[df['c'].abs().argsort()] returns:
   a   b   c
3 -8  -5  -5
0  5 -10  10
1 -4 -20  20
2  2   2 -30

❮ Pandas DataFrame - Functions

5