相关疑难解决方法(0)

重命名spark数据帧中的嵌套字段

df在Spark中拥有一个数据框:

 |-- array_field: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- a: string (nullable = true)
 |    |    |-- b: long (nullable = true)
 |    |    |-- c: long (nullable = true)
Run Code Online (Sandbox Code Playgroud)

如何将字段重命名array_field.aarray_field.a_renamed

[更新]:

.withColumnRenamed() 不适用于嵌套字段,所以我尝试了这个hacky和不安全的方法:

# First alter the schema:
schema = df.schema
schema['array_field'].dataType.elementType['a'].name = 'a_renamed'

ind = schema['array_field'].dataType.elementType.names.index('a')
schema['array_field'].dataType.elementType.names[ind] = 'a_renamed'

# Then set dataframe's schema with altered schema
df._schema = schema
Run Code Online (Sandbox Code Playgroud)

我知道设置私有属性不是一个好习惯,但我不知道为df设置架构的其他方法

我觉得我是在一个正确的轨道,但df.printSchema()仍显示为旧名array_field.a …

python rename dataframe apache-spark pyspark

8
推荐指数
2
解决办法
6414
查看次数

标签 统计

apache-spark ×1

dataframe ×1

pyspark ×1

python ×1

rename ×1