You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@spark.apache.org by "Xiao Li (JIRA)" <ji...@apache.org> on 2018/07/12 23:51:00 UTC

[jira] [Created] (SPARK-24796) Support GROUPED_AGG_PANDAS_UDF in Pivot

Xiao Li created SPARK-24796:
-------------------------------

             Summary: Support GROUPED_AGG_PANDAS_UDF in Pivot
                 Key: SPARK-24796
                 URL: https://issues.apache.org/jira/browse/SPARK-24796
             Project: Spark
          Issue Type: Improvement
          Components: PySpark, SQL
    Affects Versions: 2.4.0
            Reporter: Xiao Li


Currently, Grouped AGG PandasUDF is not supported in Pivot. It is nice to support it. 

{code}
# create input dataframe
from pyspark.sql import Row
data = [
  Row(id=123, total=200.0, qty=3, name='item1'),
  Row(id=124, total=1500.0, qty=1, name='item2'),
  Row(id=125, total=203.5, qty=2, name='item3'),
  Row(id=126, total=200.0, qty=500, name='item1'),
]
df = spark.createDataFrame(data)

from pyspark.sql.functions import pandas_udf, PandasUDFType

@pandas_udf('double', PandasUDFType.GROUPED_AGG)
def pandas_avg(v):
   return v.mean()

from pyspark.sql.functions import col, sum
  
applied_df = df.groupby('id').pivot('name').agg(pandas_avg('total').alias('mean'))

applied_df.show()

{code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@spark.apache.org
For additional commands, e-mail: issues-help@spark.apache.org