pyspark中groupBy之后的列别名 [英] Column alias after groupBy in pyspark

查看:38
本文介绍了pyspark中groupBy之后的列别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要下一行中的结果数据框,以便在 groupBy 之后为 max('diff') 列设置别名maxDiff".但是,下面的行不会进行任何更改,也不会引发错误.

I need the resulting data frame in the line below, to have an alias name "maxDiff" for the max('diff') column after groupBy. However, the below line does not makeany change, nor throw an error.

 grpdf = joined_df.groupBy(temp1.datestamp).max('diff').alias("maxDiff")

推荐答案

您可以使用 agg 而不是调用 max 方法:

You can use agg instead of calling max method:

from pyspark.sql.functions import max

joined_df.groupBy(temp1.datestamp).agg(max("diff").alias("maxDiff"))

在 Scala 中类似

Similarly in Scala

import org.apache.spark.sql.functions.max

joined_df.groupBy($"datestamp").agg(max("diff").alias("maxDiff"))

joined_df.groupBy($"datestamp").agg(max("diff").as("maxDiff"))

这篇关于pyspark中groupBy之后的列别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆