Get Absolute value of column in Pyspark

In order to get Absolute value of column in pyspark we use abs() function. abs() function in pyspark gets the absolute value of the column. Let’s see how to

  • Extract absolute value in pyspark
  • Extracts the absolute value of the column in pyspark

With an example for both

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

Get absolute value of the column in pyspark 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()

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

Get absolute value of the column in pyspark 2

 

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