PySpark 1.5 Groupby Sum 用于 Dataframe 中的新列
PySpark 1.5 Groupby Sum for new column in Dataframe
我正在尝试使用 groupBy 和 sum(使用 PySpark 1.5)在 Spark Dataframe 中创建一个新列 ("newaggCol")。我的数字列已转换为 Long 或 Double。用于形成 groupBy 的列是 String 和 Timestamp。我的代码如下
df= df.withColumn("newaggCol",(df.groupBy([df.strCol,df.tsCol]).sum(df.longCol)))
我对错误的回溯是到那一行。并说明:
ValueError: Cannot convert column into bool: please use '&' for 'and', '|' for 'or', '~' for 'not' when building DataFrame boolean expressions.
我觉得我一定是调用函数不正确?
无法使用 SQL 聚合,但您可以使用 window 函数轻松获得所需结果
import sys
from pyspark.sql.window import Window
from pyspark.sql.functions import sum as sum_
w = (Window()
.partitionBy(df.strCol, df.tsCol)
.rowsBetween(Window.unboundedPreceding, Window.unboundedFollowing))
df.withColumn("newaggCol", sum_(df.longCol).over(w))
我正在尝试使用 groupBy 和 sum(使用 PySpark 1.5)在 Spark Dataframe 中创建一个新列 ("newaggCol")。我的数字列已转换为 Long 或 Double。用于形成 groupBy 的列是 String 和 Timestamp。我的代码如下
df= df.withColumn("newaggCol",(df.groupBy([df.strCol,df.tsCol]).sum(df.longCol)))
我对错误的回溯是到那一行。并说明:
ValueError: Cannot convert column into bool: please use '&' for 'and', '|' for 'or', '~' for 'not' when building DataFrame boolean expressions.
我觉得我一定是调用函数不正确?
无法使用 SQL 聚合,但您可以使用 window 函数轻松获得所需结果
import sys
from pyspark.sql.window import Window
from pyspark.sql.functions import sum as sum_
w = (Window()
.partitionBy(df.strCol, df.tsCol)
.rowsBetween(Window.unboundedPreceding, Window.unboundedFollowing))
df.withColumn("newaggCol", sum_(df.longCol).over(w))