You can use the native Spark function abs():

from  pyspark.sql.functions import abs

df1 = df.withColumn('Value',abs(df.Value))
df1.show()
+---+-----+
| Id|Value|
+---+-----+
|  a|  5.0|
|  b|  1.0|
|  c|  0.3|
+---+-----+
Answer from mtoto on Stack Overflow
🌐
Apache
spark.apache.org › docs › latest › api › python › reference › pyspark.sql › api › pyspark.sql.functions.abs.html
pyspark.sql.functions.abs - Apache Spark
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.
🌐
DataScience Made Simple
datasciencemadesimple.com › home › absolute value of column in pyspark – abs() function
Absolute value of column in Pyspark - abs() function - DataScience Made Simple
November 17, 2024 - ########## Extract Absolute value of the column in pyspark from pyspark.sql.functions import abs df1 = df_states.withColumn('Absolute_Value',abs(df_states.hindex_score)) df1.show() Importing the necessary libraries: SparkSession for creating a Spark session and abs for calculating absolute values.
🌐
Sparkreference
sparkreference.com › reference › abs
Introduction to the abs() function in PySpark - Spark Reference
It returns the non-negative value of the input, regardless of its original sign. The primary purpose of the abs() function is to transform data by removing any negative signs and converting negative values to positive ones.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › microsoft.spark.sql.functions.abs
Functions.Abs(Column) Method (Microsoft.Spark.Sql) - .NET for Apache Spark | Microsoft Learn
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here. Computes the absolute value. public static Microsoft.Spark.Sql.Column Abs(Microsoft.Spark.Sql.Column column);
🌐
Medium
medium.com › @uzzaman.ahmed › pyspark-mathematical-functions-a-comprehensive-guide-6436921fc55f
PySpark Mathematical Functions: A Comprehensive Guide | by Ahmed Uz Zaman | Medium
March 20, 2023 - Examples of mathematical functions in PySpark include: abs(col): Returns the absolute value of the given value. from pyspark.sql.functions import abs # Sample data data = [(1,), (-2,), (3,), (-4,)] df = spark.createDataFrame(data, ["value"]) ...
🌐
Databricks Documentation
docs.databricks.com › reference › sql language reference › functions › built-in functions › alphabetical list of built-in functions › abs function
abs function | Databricks on AWS
November 14, 2022 - Returns the absolute value of the numeric value in expr. ... A numeric or interval of the same type as expr. For integral numeric types the function can return an ARITHMETIC_OVERFLOW error.
Find elsewhere
🌐
Pysparkisrad
pysparkisrad.com › functions › abs
abs | PySpark Is Rad
from pyspark.sql import functions as F # Set up dataframe data = [{"num": 1},{"num": -2},{"num": 0}] df = spark.createDataFrame(data) # Use function df = (df .withColumn("absolute", F.abs("num")) ) df.show() Usage: This is just a basic math function. Nothing special about it. I’ve used it before when doing subtraction between two columns. returns: \_invoke_function_over_column("abs", col) PySpark manual ·
Top answer
1 of 2
2

That's not how you use pyspark.sql.functions. There are not designed to be evaluated outside DataFrame context, and operate on Columns.

You could use literal Column:

from pyspark.sql.functions import abs, lit 

abs(lit(numb))

but it'll give you yet another Column:

Column<b'abs(-2)'>

While in theory such objects can be evaluated locally, it is not intended for public usage.

If you want to operate on plain Python numerics just stick to Python's built-in abs.

If you've shaded built-in functions you can express the function from the comments as:

def math_result(current_val, value): 
    result = ((value - current_val) / value)  *100 
    return __builtins__.abs(__builtins__.round(result, 2)) 

math_result(1, 3)                                            
## 66.67
2 of 2
0

Python has built-in abs method.

pyspark also provides abs method but that is for DataFrame column.

If you do import pyspark method 'abs' in pyspark shell then you do override built-in abs method.

It looks like you have override abs method something like below:

>>> print(abs(-3))
3
>>> from pyspark.sql.functions import abs
>>> print(abs(-3))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/rs301t/spark-2.3.2-bin-hadoop2.7/python/pyspark/sql/functions.py", line 42, in _
    jc = getattr(sc._jvm.functions, name)(col._jc if isinstance(col, Column) else col)
  File "/Users/rs301t/spark-2.3.2-bin-hadoop2.7/python/lib/py4j-0.10.7-src.zip/py4j/java_gateway.py", line 1257, in __call__
  File "/Users/rs301t/spark-2.3.2-bin-hadoop2.7/python/pyspark/sql/utils.py", line 63, in deco
    return f(*a, **kw)
  File "/Users/rs301t/spark-2.3.2-bin-hadoop2.7/python/lib/py4j-0.10.7-src.zip/py4j/protocol.py", line 332, in get_return_value
py4j.protocol.Py4JError: An error occurred while calling z:org.apache.spark.sql.functions.abs. Trace:
py4j.Py4JException: Method abs([class java.lang.Integer]) does not exist
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318)
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:339)
    at py4j.Gateway.invoke(Gateway.java:276)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)

We should avoid import functions name directly and try to use alias for module so can call right method which we intended.

🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas dataframe abs() method
Pandas DataFrame abs() Method - Spark By {Examples}
December 6, 2024 - In pandas, the abs() method is used to return a DataFrame with the absolute value of each element. It is applicable to both Series and DataFrames. This
🌐
Apache Spark
spark.apache.org › docs › latest › api › sql › index.html
Spark SQL, Built-in Functions
When SQL config 'spark.sql.parser.escapedStringLiterals' is enabled, it falls back to Spark 1.6 behavior regarding string literal parsing. For example, if the config is enabled, the pattern to match "\abc" should be "\abc". It's recommended to use a raw string literal (with the r prefix) to avoid escaping special characters in the pattern string if exists.
🌐
Databricks Documentation
docs.databricks.com › reference › sql language reference › functions › built-in functions › alphabetical list of built-in functions › abs function
abs function | Databricks on Google Cloud
April 18, 2024 - Returns the absolute value of the numeric value in expr. ... A numeric or interval of the same type as expr. For integral numeric types the function can return an ARITHMETIC_OVERFLOW error.
🌐
Apache
spark.apache.org › docs › latest › api › python › reference › api › pyspark.ml.feature.MaxAbsScaler.html
MaxAbsScaler — PySpark 4.1.1 documentation - Apache Spark
Rescale each feature individually to range [-1, 1] by dividing through the largest maximum absolute value in each feature. It does not shift/center the data, and thus does not destroy any sparsity. New in version 2.0.0. Examples · >>> from pyspark.ml.linalg import Vectors >>> df = spark.createDataFrame([(Vectors.dense([1.0]),), (Vectors.dense([2.0]),)], ["a"]) >>> maScaler = MaxAbsScaler(outputCol="scaled") >>> maScaler.setInputCol("a") MaxAbsScaler...
🌐
700R4 Transmission
700r4transmissionhq.com › home › chevy spark › chevy spark: abs light meaning, diagnosis, + how to fix
Chevy Spark: ABS Light Meaning, Diagnosis, + How to Fix | Drivetrain Resource
January 10, 2023 - If the fluid reservoir level is too low to keep pressure in the ABS lines, the ABS light will come on. Without the proper pressure, the ABS system will no longer be able to activate physically.
🌐
Microsoft Learn
learn.microsoft.com › en-us › azure › databricks › sql › language-manual › functions › abs
abs function - Azure Databricks - Databricks SQL | Microsoft Learn
April 18, 2024 - Returns the absolute value of the numeric value in expr. ... A numeric or interval of the same type as expr. For integral numeric types the function can return an ARITHMETIC_OVERFLOW error.