Home > Enterprise >  Python - pyspark - Dataframe withcolumn
Python - pyspark - Dataframe withcolumn

Time:09-24

I have a pyspark dataframe. I am trying to add additional columns.

I keep getting "AssertionError col should be Column"

I have tried casting to string.. then error : "str object does not have an attribute cast"


df = df.withColumn("SomeNumber",lit(123)) ## this works
df = df.withColumn("SomeString","why not!") ## this does not work 


CodePudding user response:

withColumn() expects a column as the second attribute. Thats why you need the lit() function which creates a column from your literal.

  • Related