Search code examples
pythonpysparkabsolute-value

How to get absolute value in pyspark?


I am trying to run pyspark script. I am trying to find % difference between two count but not able to get the proper value. Can you please help me how to get this?

Example:

pre_count = 100
cur_count = 25

diff = (float((cur_count - pre_count)/pre_count)*100)

diff is giving -100.0

expected output: -25


Solution

  • It seems like your formula is a bit off, to calculate percentage of decrease, do:

    |100 - 25|/100 = 0.75 = 75%
    

    Which would translate to

    pre_count = 100
    cur_count = 25
    diff = (abs((pre_count - cur_count )/pre_count )*100)
    

    I am using python abs() to get the absolute difference.