Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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
如何在pyspark中将列传递给substr函数_Pyspark_Apache Spark Sql - Fatal编程技术网

如何在pyspark中将列传递给substr函数

如何在pyspark中将列传递给substr函数,pyspark,apache-spark-sql,Pyspark,Apache Spark Sql,我在数据框中有两列,ValueText和GLength。我需要在其他两列(ValueText和GLength)的基础上添加一个新的VX列。基本上,新列VX基于ValueText的子字符串。下面是我试过的 df_stage1.withColumn("VX", df_stage1.ValueText.substr(6,df_stage1.GLength)) 然而,对于上面的代码,我得到了一个错误:startPos和length必须是相同的类型。分别获得了类“int”和类“pyspark.sql.

我在数据框中有两列,ValueText和GLength。我需要在其他两列(ValueText和GLength)的基础上添加一个新的VX列。基本上,新列VX基于ValueText的子字符串。下面是我试过的

df_stage1.withColumn("VX", df_stage1.ValueText.substr(6,df_stage1.GLength)) 
然而,对于上面的代码,我得到了一个错误:startPos和length必须是相同的类型。分别获得了类“int”和类“pyspark.sql.column.column”

我也试过了

func.expr("substring(ValueText,5, 5 + GLength)")

当我执行上述代码时,我得到一个错误:Pyspark作业由于阶段故障而中止

expr
将在这种情况下工作,因为我们在子字符串函数中使用了
Glength

示例:

df=spark.createDataFrame([("abcdff",4),("dlaldajfa",3)],["valuetext","Glength"])
df.show()
#+---------+-------+
#|valuetext|Glength|
#+---------+-------+
#|   abcdff|      4|
#|dlaldajfa|      3|
#+---------+-------+

from pyspark.sql.functions import *

df.withColumn("vx",expr("substring(valuetext,0,Glength)")).show()
#+---------+-------+----+
#|valuetext|Glength|  vx|
#+---------+-------+----+
#|   abcdff|      4|abcd|
#|dlaldajfa|      3| dla|
#+---------+-------+----+

你能试试df_stage1.ValueText.substr(f.lit(6),df_stage1.GLength)吗?它起作用了。我认为问题在于我的进口声明:-(