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
- col
Column
or column name The target column or expression to compute the absolute value on.
- col
- Returns
Column
A new column object representing the absolute value of the input.
Examples
Example 1: Compute the absolute value of a long column
>>> from pyspark.sql import functions as sf >>> df = spark.createDataFrame([(-1,), (-2,), (-3,), (None,)], ["value"]) >>> df.select("*", sf.abs(df.value)).show() +-----+----------+ |value|abs(value)| +-----+----------+ | -1| 1| | -2| 2| | -3| 3| | NULL| NULL| +-----+----------+
Example 2: Compute the absolute value of a double column
>>> from pyspark.sql import functions as sf >>> df = spark.createDataFrame([(-1.5,), (-2.5,), (None,), (float("nan"),)], ["value"]) >>> df.select("*", sf.abs(df.value)).show() +-----+----------+ |value|abs(value)| +-----+----------+ | -1.5| 1.5| | -2.5| 2.5| | NULL| NULL| | NaN| NaN| +-----+----------+
Example 3: 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() +---+-----+-----------------+ | id|value|abs((id - value))| +---+-----+-----------------+ | 1| 1| 0| | 2| -2| 4| | 3| 3| 0| +---+-----+-----------------+