pyspark.sql.functions.abs#

pyspark.sql.functions.abs(col)[source]#

Mathematical Function: Computes the absolute value of the given column or expression.

New in version 1.3.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
colColumn or str

The target column or expression to compute the absolute value on.

Returns
Column

A new column object representing the absolute value of the input.

Examples

Example 1: Compute the absolute value of a negative number

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([(1, -1), (2, -2), (3, -3)], ["id", "value"])
>>> df.select(sf.abs(df.value)).show()
+----------+
|abs(value)|
+----------+
|         1|
|         2|
|         3|
+----------+

Example 2: Compute the absolute value of an expression

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([(1, 1), (2, -2), (3, 3)], ["id", "value"])
>>> df.select(sf.abs(df.id - df.value)).show()
+-----------------+
|abs((id - value))|
+-----------------+
|                0|
|                4|
|                0|
+-----------------+

Example 3: Compute the absolute value of a column with null values

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([(1, None), (2, -2), (3, None)], ["id", "value"])
>>> df.select(sf.abs(df.value)).show()
+----------+
|abs(value)|
+----------+
|      NULL|
|         2|
|      NULL|
+----------+

Example 4: Compute the absolute value of a column with double values

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([(1, -1.5), (2, -2.5), (3, -3.5)], ["id", "value"])
>>> df.select(sf.abs(df.value)).show()
+----------+
|abs(value)|
+----------+
|       1.5|
|       2.5|
|       3.5|
+----------+