如何在groupby之后的数据帧上使用collect_set或collect_list。例如:df.groupby('key').collect_set('values')。我得到一个错误:AttributeError: 'GroupedData' object has no attribute 'collect_set'
Grouped aggregate Pandas UDFs are similar to Spark aggregate functions. Grouped aggregate Pandas UDFs are used with groupBy().agg() and pyspark.sql.Window. It defines an aggregation from one or more pandas.Series to a scalar value, where each pandas.Series represents a column within the group or window. pandas udf
示例:
import pyspark.sql.functions as F
@F.pandas_udf('string', F.PandasUDFType.GROUPED_AGG)
def collect_list(name):
return ', '.join(name)
grouped_df = df.groupby('id').agg(collect_list(df["name"]).alias('names'))
如果数据帧很大,可以尝试使用pandas udf(GROUPED_AGG)来避免内存错误。它也快得多。
示例:
你需要使用agg。示例:
注意在上面你必须创建一个HiveContext。有关处理不同Spark版本的信息,请参见https://stackoverflow.com/a/35529093/690430。
相关问题 更多 >
编程相关推荐