您可以通過從現有屬性重新創建新結構來更新結構列dates,并使用when表達式檢查所有end_dates屬性是否為空: val df2 = df.withColumn( "dates", struct( col("dates.start_date"), // keep start_date when( Seq("year", "month", "day") .map(x => col(s"dates.end_date.$x").isNull) .reduce(_ and _), lit(null).cast("struct<year:int,month:int,day:int>") ).otherwise(col("dates.end_date")).alias("end_date") // set end_date to null if all attr are null ))df2.show(false)//+----+--------------------------------+//|name|dates |//+----+--------------------------------+//|A |[[1994, 12, 11],] |//|B |[[1994, 12, 11], [1994, 12, 25]]|//+----+------------