Absolute value of column in Pyspark – abs() function

In PySpark, you can compute the absolute value of a column in a DataFrame using the abs() function. This function is part of the PySpark.sql.functions module. Absolute method in PySpark – abs() function in PySpark gets the absolute value of the numeric column . Let’s see how to

  • Extract absolute value in pyspark using abs() function.
  • Extracts the absolute value of the column using abs() method.

With an example for both

Get absolute value of column in Pyspark 1

 

Get Absolute value in Pyspark:

abs() function in pyspark gets the absolute value

abs(-113.5)
abs(113.5)

So the Absolute value will be

113.5

 

 

Extract Absolute value of the column in Pyspark:

To get absolute value of the column in pyspark, we will using abs() function and passing column as an argument to that function. Lets see with an example the dataframe that we use is df_states

Absolute value of column in Pyspark - abs() function 1

abs() function takes column as an argument and gets absolute value of that column

########## 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()

Explanation:

Importing the necessary libraries: SparkSession for creating a Spark session and abs for calculating absolute values.

Calculating absolute values: The withColumn method is used to add a new column “Absolute_Value”, which contains the absolute values of the “hindex_score” column.

Displaying results: The updated DataFrames are displayed.

so the resultant absolute column of “hindex_score” is calculated as shown below

Absolute value of column in Pyspark - abs() function 2

 


Other Related Topics:

 

 

Get Absolute value of column in Pyspark                                                                                               Get Absolute value of column in Pyspark

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts