Absolute value of column in Pyspark – abs() function

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.  Absolute method in pyspark – abs(), computes the absolute value of numeric data. 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

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

 


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