scala - 转换包含在数组列中的列
问题描述
我需要在我的数据框中转换一个数组列,数组称为“城市”,类型为 Array(City),我想将城市名称大写。
结构:
val cities: StructField = StructField("cities", ArrayType(CityType), nullable = true)
def CityType: StructType =
StructType(
Seq(
StructField(code, StringType, nullable = true),
StructField(name, StringType, nullable = true)
)
)
我试过的代码:
.withColumn(
newColumn,
forall(
col(cities),
(col: Column) =>
struct(
Array(
col(code),
upper(col(name))
): _*
)
)
)
错误说
无法解决'forall(...
解决方案
没有这种东西叫做forall
。您可以transform
改用:
// sample data
val df = spark.sql("select array(struct('1' as code, 'abc' as name), struct('2' as code, 'def' as name)) cities")
import org.apache.spark.sql.Column
val df2 = df.withColumn(
"newcol",
transform(
col("cities"),
(c: Column) => struct(c("code"), upper(c("name")))
)
)
df2.show
+--------------------+--------------------+
| cities| newcol|
+--------------------+--------------------+
|[[1, abc], [2, def]]|[[1, ABC], [2, DEF]]|
+--------------------+--------------------+
推荐阅读
- jquery - 未捕获的 TypeError:$.ajax(...).error 不是 rails 中的函数
- flutter - FormatException:Flutterinsta 包中的意外字符(在字符 1 处)
- python - 根据值从嵌套字典中检索键,其中键名未知
- php - 如何从图形 facebook API 获取匹配的记录?
- openssh - ssh "packet_write_wait: Connection to xxxx port 22: Broken pipe" -- 源代码在哪里?
- laravel - 我通过 Ajax 发表评论,但它不起作用
- java - java将字符串数据映射到python中的字典
- spring-boot - 如何在 azure devops 构建管道中填充 spring.active.profiles 值
- python - 堆化后的 Python 排序算法 Visualiser 堆排序错误(Tkinter)
- python - 使用另一个数据帧行中的多个值在熊猫数据帧中查找值