Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyspark - How to get basic stats (mean, min, max) along with quantiles (25%, 50%) for numerical cols in a single dataframe

I have a spark df

spark_df = spark.createDataFrame(
    [(1, 7, 'foo'), 
     (2, 6, 'bar'),
     (3, 4, 'foo'),
     (4, 8, 'bar'),
     (5, 1, 'bar')
    ],
    ['v1', 'v2', 'id'] 
)

Expected Output

    id  avg(v1)   avg(v2)   min(v1) min(v2) 0.25(v1)     0.25(v2)    0.5(v1)     0.5(v2)
0   bar 3.666667    5.0     2        1       some-value  some-value  some-value  some-value
1   foo 2.000000    5.5     1        4.      some-value  some-value  some-value  some-value

Until, now I can achieve the basic stats like avg, min, max. But not able to get the quantiles. I know ,this can be achieved easily in Pandas but not able to get it done in Pyspark

Also, I knew about approxQuantile, but I am not able to combine basic stasts along with quantiles in pyspark

Until now, I can get the basic stats like mean and min by using agg. Also I want quantiles in the same df

func = [F.mean, F.min,]
NUMERICAL_FEATURE_LIST = ['v1', 'v2']
GROUP_BY_FIELDS = ['id']
exp = [f(F.col(c)) for f in func for c in NUMERICAL_FEATURE_LIST]
df_fin = spark_df.groupby(*GROUP_BY_FIELDS).agg(*exp)

like image 210
user1994 Avatar asked Jan 31 '26 05:01

user1994


1 Answers

Perhaps this is helpful-

 val spark_df = Seq((1, 7, "foo"),
      (2, 6, "bar"),
      (3, 4, "foo"),
      (4, 8, "bar"),
      (5, 1, "bar")
    ).toDF("v1", "v2", "id")
    spark_df.show(false)
    spark_df.printSchema()
    spark_df.summary() // default= "count", "mean", "stddev", "min", "25%", "50%", "75%", "max"
      .show(false)

    /**
      * +---+---+---+
      * |v1 |v2 |id |
      * +---+---+---+
      * |1  |7  |foo|
      * |2  |6  |bar|
      * |3  |4  |foo|
      * |4  |8  |bar|
      * |5  |1  |bar|
      * +---+---+---+
      *
      * root
      * |-- v1: integer (nullable = false)
      * |-- v2: integer (nullable = false)
      * |-- id: string (nullable = true)
      *
      * +-------+------------------+------------------+----+
      * |summary|v1                |v2                |id  |
      * +-------+------------------+------------------+----+
      * |count  |5                 |5                 |5   |
      * |mean   |3.0               |5.2               |null|
      * |stddev |1.5811388300841898|2.7748873851023217|null|
      * |min    |1                 |1                 |bar |
      * |25%    |2                 |4                 |null|
      * |50%    |3                 |6                 |null|
      * |75%    |4                 |7                 |null|
      * |max    |5                 |8                 |foo |
      * +-------+------------------+------------------+----+
      */

if you need in the format, then use below answer.

like image 171
Som Avatar answered Feb 03 '26 05:02

Som