Search code examples
pythondataframepysparkboolean

How to assign a column to be True or False boolean in a pyspark dataframe


My current code to assign a boolean value to my pyspark dataframe is: df = df.withColumn('my_column_name', True)

However, I get the error: "AssertionError: col should be Column"

Do I need to have "True" value wrapped with col(BooleanType(True))? I don't think I should be casting it as a lit string


Solution

  • You can try:

    df = df.withColumn("my_column_name", F.lit(1).cast('boolean'))