如何在pyspark中将结构数据类型更改为整数?

如何在pyspark中将结构数据类型更改为整数?,pyspark,apache-spark-sql,Pyspark,Apache Spark Sql,我有一个数据帧df,一列的数据类型为struct 由于这种数据类型结构,我无法执行加法、减法等 如何将struct更改为仅IntegerType?您可以使用点语法访问struct列的部分内容 例如,如果您从这个数据帧开始 df=spark.createDataFrame([(1,(3,'x'),(4,(8,'y'))]).toDF(“col1”,“col2”) df.show() df.printSchema() +------+ |col1 | col2| +----+------+ |1

我有一个数据帧df,一列的数据类型为
struct

由于这种数据类型结构,我无法执行加法、减法等


如何将
struct
更改为仅
IntegerType

您可以使用点语法访问struct列的部分内容

例如,如果您从这个数据帧开始

df=spark.createDataFrame([(1,(3,'x'),(4,(8,'y'))]).toDF(“col1”,“col2”)
df.show()
df.printSchema()
+------+
|col1 | col2|
+----+------+
|1 |[3,x]|
|4 |[8,y]|
+----+------+
根
|--col1:long(nullable=true)
|--col2:struct(nullable=true)
||--_1:long(nullable=true)
||--_2:string(nullable=true)
用户可以选择结构列的第一部分,并创建新列或替换现有列:

df.withColumn('col2',df['col2.\u 1']).show()
印刷品

+----+----+
|col1 | col2|
+----+----+
|   1|   3|
|   4|   8|
+----+----+

只需选择
df['col\u name']。long