PySpark 转换为数组类型

PySpark Conversion to Array Types

我目前正在处理以下错误,同时尝试 运行 pyspark.sql.functions.explode PySpark 中 DataFrame 中的数组列。我已经尝试创建一个 UDF 以将列转换为 python 列表(如果它不是列表实例)。但是,这仍然会引发相同的错误。在 Pandas 中,我通常会拉出该行并从那里确定要做什么。我不确定如何访问此行来查看数据以了解我需要考虑的条件。

总的来说,我更想寻求调试建议,但如果您知道答案,那也很好!

Py4JJavaError: An error occurred while calling o2850.withColumn. : org.apache.spark.sql.AnalysisException: cannot resolve 'explode(很多)' due to data type mismatch: input to function explode should be array or map type, not LongType;;

df.schema()

root
 |-- lists: array (nullable = true)
 |    |-- element: long (containsNull = true)
 |-- data: string (nullable=true)

df = df.withColumn("list",df.lists)
df = df.withColumn('list',sf.explode(df.list))

原代码

from pyspark.sql import functions as sf 

# create duplicate column to use with explode 
# explode the array datetype into multiple rows per element 

df = spark.read("s3a://path/parquet/*")     
df = df.withColumn("list",df.lists)
df = df.withColumn('list',sf.explode(df.list)) 

不用withcolumn可以直接爆数组

df = spark.read("s3a://path/parquet/*")     
df.select(df['data'],explode(df['lists']).alias('list'))