Add an empty column to Spark DataFrame

后端 未结 2 1948
春和景丽
春和景丽 2020-12-13 17:23

As mentioned in many other locations on the web, adding a new column to an existing DataFrame is not straightforward. Unfortunately it is important to have this functionalit

2条回答
  •  轮回少年
    2020-12-13 17:50

    I would cast lit(None) to NullType instead of StringType. So that if we ever have to filter out not null rows on that column...it can be easily done as follows

    df = sc.parallelize([Row(1, "2"), Row(2, "3")]).toDF()
    
    new_df = df.withColumn('new_column', lit(None).cast(NullType()))
    
    new_df.printSchema() 
    
    df_null = new_df.filter(col("new_column").isNull()).show()
    df_non_null = new_df.filter(col("new_column").isNotNull()).show()
    

    Also be careful about not using lit("None")(with quotes) if you are casting to StringType since it would fail for searching for records with filter condition .isNull() on col("new_column").

提交回复
热议问题