0

I have two piece of codes here

gooddata = gooddata.withColumn("Priority",when(gooddata.years_left < 5 & (gooddata.Years_left >= 0),lit("CRITICAL"))).fillna("LOW").show(5)

gooddata=gooddata.withColumn("Priority",when((gooddata.Years_left < 5) & (gooddata.Years_left >= 0),"CRITICAL").otherwise("LOW")).show(5)
thebluephantom
  • 16,458
  • 8
  • 40
  • 83
  • 1
    Does this answer your question? [Where do you need to use lit() in Pyspark SQL?](https://stackoverflow.com/questions/37715060/where-do-you-need-to-use-lit-in-pyspark-sql) – user10938362 Jun 09 '20 at 17:59

1 Answers1

1

For both spark and pyspark:

  • literals in certain statements
  • comparing with nulls
  • getting the name of a dataframe column instead of the contents of the dataframe column

E.g.

val nonNulls = df.columns.map(x => when(col(x).isNotNull, concat(lit(","), lit(x))).otherwise(",")).reduce(concat(_, _))

from question: Add a column to spark dataframe which contains list of all column names of the current row whose value is not null

val df2 = df.select(col("EmpId"),col("Salary"),lit("1").as("lit_value1"))
thebluephantom
  • 16,458
  • 8
  • 40
  • 83