Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache spark 在pyspark dataframe中添加最大值为另一列的新列_Apache Spark_Pyspark - Fatal编程技术网

Apache spark 在pyspark dataframe中添加最大值为另一列的新列

Apache spark 在pyspark dataframe中添加最大值为另一列的新列,apache-spark,pyspark,Apache Spark,Pyspark,需要一些关于pyspark df的帮助。我试图将另一列的最大值的新列附加到现有数据帧,但出现以下错误。这就是我正在做的 df1 = df.withColumn('WEEK_START_DATE', df.agg(f.max('DATE'))) error: AttributeError: 'DataFrame' object has no attribute '_get_object_id' 我不认为我们可以在withColumn中使用聚合函数,但这里是本例的解决方法 1.使用交叉连接:

需要一些关于pyspark df的帮助。我试图将另一列的最大值的新列附加到现有数据帧,但出现以下错误。这就是我正在做的

df1 = df.withColumn('WEEK_START_DATE', df.agg(f.max('DATE')))



error:
AttributeError: 'DataFrame' object has no attribute '_get_object_id'
我不认为我们可以在withColumn中使用聚合函数,但这里是本例的解决方法

1.使用交叉连接:

二,。使用窗口功能:

三,。使用变量替换:

我不认为我们可以在withColumn中使用聚合函数,但这里是本例的解决方法

1.使用交叉连接:

二,。使用窗口功能:

三,。使用变量替换:


变量替换抛出以下错误。`assert allisinstancec,表达式中c的列,所有表达式都应该是列AssertionError:所有表达式都应该是列'Try with max_value=litdf.aggmaxid.collect[0][0],df.with columnMax,max_value。show@ben,是,请使用Spark sql部分检查更新的答案。变量替换引发以下错误。`assert allisinstancec,表达式中c的列,所有表达式都应该是列AssertionError:所有表达式都应该是列'Try with max_value=litdf.aggmaxid.collect[0][0],df.with columnMax,max_value。show@ben,是的,请使用Spark sql部分检查更新的答案。
from pyspark.sql.functions import *
df.show()    
#+---+----+
#| id|name|
#+---+----+
#|  1|   a|
#|  2|   b|
#|  3|   c|
#+---+----+
df1=df.agg(max('id'))
spark.sql("set spark.sql.crossJoin.enabled=true")
#cross join
df.join(df1)
#or
df.crossJoin(df1).show()
+---+----+-------+
#| id|name|max(id)|
#+---+----+-------+
#|  1|   a|      3|
#|  2|   b|      3|
#|  3|   c|      3|
#+---+----+-------+
from pyspark.sql import *
import sys
w=Window.orderBy(monotonically_increasing_id()).rowsBetween(-sys.maxsize,sys.maxsize)
df.withColumn("max",max(col("id")).over(w)).show()
#+---+----+---+
#| id|name|max|
#+---+----+---+
#|  1|   a|  3|
#|  2|   b|  3|
#|  3|   c|  3|
#+---+----+---+
max_value=df.agg(max("id")).collect()[0][0]

df.withColumn("max",lit(max_value)).show()

#or
max_value=lit(df.agg(max("id")).collect()[0][0])
type(max_value)
#<class 'pyspark.sql.column.Column'>
df.withColumn("max",max_value).show()
#+---+----+---+
#| id|name|max|
#+---+----+---+
#|  1|   a|  3|
#|  2|   b|  3|
#|  3|   c|  3|
#+---+----+---+
df.createOrReplaceTempView("tmp")
spark.sql("select * from tmp cross join (select max(id) max_val from tmp) t1").show()

spark.sql("select *,max(id) over(order by id rows between unbounded preceding and unbounded following) as max_val from tmp").show()

max_value=df.agg(max(col("id"))).collect()[0][0]
spark.sql("select *,{0} as max_val from tmp".format(max_value)).show()
#+---+----+-------+
#| id|name|max_val|
#+---+----+-------+
#|  1|   a|      3|
#|  2|   b|      3|
#|  3|   c|      3|
#+---+----+-------+