How To Create A Z-score In Spark Sql For Each Group
I have a dataframe which looks like this dSc TranAmount 1: 100021 79.64 2: 100021 79.64 3: 100021 0.16 4: 100022 11.65 5: 100022 0.36
Solution 1:
You can for example compute statistics and join
with the original data:
stats = (df.groupBy("dsc")
.agg(
func.stddev_pop("TranAmount").alias("sd"),
func.avg("TranAmount").alias("avg")))
df.join(broadcast(stats), ["dsc"])
(df
.join(func.broadcast(stats), ["dsc"])
.select("dsc", "TranAmount", (df.TranAmount - stats.avg) / stats.sd))
or use window functions with standard deviation formula:
from pyspark.sql.window import Window
import sys
def z_score_w(col, w):
avg_ = func.avg(col).over(w)
avg_sq = func.avg(col * col).over(w)
sd_ = func.sqrt(avg_sq - avg_ * avg_)
return (col - avg_) / sd_
w = Window().partitionBy("dsc").rowsBetween(-sys.maxsize, sys.maxsize)
df.withColumn("zscore", z_score_w(df.TranAmount, w))
Post a Comment for "How To Create A Z-score In Spark Sql For Each Group"