PySpark中的Groupby和UDF / UDAF,同时保持DataFrame结构

时间:2018-10-28 13:02:19

标签: python apache-spark dataframe pyspark apache-spark-sql

我是PySpark的新手,正在尝试简单的数据框操作。我有一个类似于的数据框:

product    period     rating   product_Desc1   product_Desc2 ..... more columns 
a            1         60          foo              xx
a            2         70          foo              xx
a            3         59          foo              xx
b            1         50          bar              yy
b            2         55          bar              yy
c            1         90          foo bar          xy
c            2         100         foo bar          xy

我想按产品分组,添加列以计算算术,几何和调和等级的平均值,同时还要保持数据框中的其余列,它们在每个产品中都保持一致。< / p>

我试图通过内置函数和UDF的组合来做到这一点。例如:

a_means = df.groupBy("product").agg(mean("rating").alias("a_mean")
g_means = df.groupBy("product").agg(udf_gmean("rating").alias("g_mean")

其中:

def g_mean(x):
  gm = reduce(mul,x)**(1/len(x))
  return gm

udf_gmean = udf(g_mean, FloatType())

然后,我将a_means和g_means输出与产品上的原始数据帧合并,并删除重复项。但是,对于g_means,此方法返回错误,指出groupBy中未涉及“评级”,也不是用户定义的聚合函数。...

我也尝试过使用SciPy的gmean模块,但是我收到的错误消息指出ufunc'log'不适合输入类型,尽管据我所知,所有rating列都是整数类型。

该网站上也有类似的问题,但我找不到能解决此问题的任何东西。我真的很感谢帮助,这使我发疯了!

预先感谢,如果我还没有提供足够的信息,我今天应该能够迅速提供任何进一步的信息。

值得注意的是,为了提高效率,我无法像使用Pandas数据框那样简单地转换为Pandas并进行转换...而且我正在使用Spark 2.2并且无法更新!

2 个答案:

答案 0 :(得分:1)

这样的事情怎么样

from pyspark.sql.functions import avg
df1 = df.select("product","rating").rdd.map(lambda x: (x[0],(1.0,x[1]*1.0))).reduceByKey(lambda x,y: (x[0]+y[0], x[1]*y[1])).toDF(['product', 'g_mean'])
gdf = df1.select(df1['product'],pow(df1['g_mean._2'],1.0/df1['g_mean._1']).alias("rating_g_mean"))
display(gdf)

+-------+-----------------+
|product|    rating_g_mean|
+-------+-----------------+
|      a|62.81071936240795|
|      b|52.44044240850758|
|      c|94.86832980505137|
+-------+-----------------+


df1 = df.withColumn("h_mean", 1.0/df["rating"])
hdf = df1.groupBy("product").agg(avg(df1["rating"]).alias("rating_mean"), (1.0/avg(df1["h_mean"])).alias("rating_h_mean"))
sdf = hdf.join(gdf, ['product'])
display(sdf)

+-------+-----------+-----------------+-----------------+
|product|rating_mean|    rating_h_mean|    rating_g_mean|
+-------+-----------+-----------------+-----------------+
|      a|       63.0|62.62847514743051|62.81071936240795|
|      b|       52.5|52.38095238095239|52.44044240850758|
|      c|       95.0|94.73684210526315|94.86832980505137|
+-------+-----------+-----------------+-----------------+


fdf = df.join(sdf, ['product'])
display(fdf.sort("product"))


+-------+------+------+-------------+-------------+-----------+-----------------+-----------------+
|product|period|rating|product_Desc1|product_Desc2|rating_mean|    rating_h_mean|    rating_g_mean|
+-------+------+------+-------------+-------------+-----------+-----------------+-----------------+
|      a|     3|    59|          foo|           xx|       63.0|62.62847514743051|62.81071936240795|
|      a|     2|    70|          foo|           xx|       63.0|62.62847514743051|62.81071936240795|
|      a|     1|    60|          foo|           xx|       63.0|62.62847514743051|62.81071936240795|
|      b|     2|    55|          bar|           yy|       52.5|52.38095238095239|52.44044240850758|
|      b|     1|    50|          bar|           yy|       52.5|52.38095238095239|52.44044240850758|
|      c|     2|   100|      foo bar|           xy|       95.0|94.73684210526315|94.86832980505137|
|      c|     1|    90|      foo bar|           xy|       95.0|94.73684210526315|94.86832980505137|
+-------+------+------+-------------+-------------+-----------+-----------------+-----------------+

答案 1 :(得分:0)

比上述方法更简单的方法:

from spark_sklearn.group_apply import gapply
from scipy.stats.mstats import gmean
import pandas as pd

def g_mean(_, vals):
gm = gmean(vals["rating"])
return pd.DataFrame(data=[gm])

geoSchema = StructType().add("geo_mean", FloatType())

gMeans = gapply(df.groupby("product"), g_mean, geoSchema)

这将返回一个数据框,然后可以使用以下命令对其进行排序并连接到原始框上:

df_withGeo = df.join(gMeans, ["product"])

并对要添加到原始DataFrame的任何聚合类型函数列重复此过程...