在 Spark 数据框中添加可为空的列

Zyg*_*ygD 6 python nullable apache-spark apache-spark-sql pyspark

在 Spark 中,文字列在添加后不可为空:

from pyspark.sql import SparkSession, functions as F
spark = SparkSession.builder.getOrCreate()

df = spark.createDataFrame([(1,)], ['c1'])

df = df.withColumn('c2', F.lit('a'))

df.printSchema()
#  root
#   |-- c1: long (nullable = true)
#   |-- c2: string (nullable = false)
Run Code Online (Sandbox Code Playgroud)

如何创建可为空的列?

Zyg*_*ygD 9

我发现的最短方法 - using when(该otherwise子句似乎不需要):

df = df.withColumn('c2', F.when(F.lit(True), F.lit('a')))
Run Code Online (Sandbox Code Playgroud)

如果在 Scala 中:.withColumn("c2", when(lit(true), lit("a")))


完整测试结果:

from pyspark.sql import SparkSession, functions as F
spark = SparkSession.builder.getOrCreate()

df = spark.createDataFrame([(1,)], ['c1'])
df = df.withColumn('c2', F.when(F.lit(True), F.lit('a')))

df.show()
#  +---+---+
#  | c1| c2|
#  +---+---+
#  |  1|  a|
#  +---+---+

df.printSchema()
#  root
#   |-- c1: long (nullable = true)
#   |-- c2: string (nullable = true)
Run Code Online (Sandbox Code Playgroud)