How to overwrite entire existing column in Spark dataframe with new column?

前端 未结 3 1357
抹茶落季
抹茶落季 2021-02-20 04:19

I want to overwrite a spark column with a new column which is a binary flag.

I tried directly overwriting the column id2 but why is it not working like a inplace operati

3条回答
  •  [愿得一人]
    2021-02-20 05:09

    You can use

    d1.withColumnRenamed("colName", "newColName")
    d1.withColumn("newColName", $"colName")
    

    The withColumnRenamed renames the existing column to new name.

    The withColumn creates a new column with a given name. It creates a new column with same name if there exist already and drops the old one.

    In your case changes are not applied to the original dataframe df2, it changes the name of column and return as a new dataframe which should be assigned to new variable for the further use.

    d3 = df2.select((df2.id2 > 0).alias("id2"))
    

    Above should work fine in your case.

    Hope this helps!

提交回复
热议问题