1

I have a dataframe and i need to compare the value of a column. The value of the column should be greater than 2 but less than 6 How should i achieve that

df.withColumn('coln',(min(max(df["cola"],2),6)))

This doesn't work

Nick Ryan
  • 19
  • 5
  • you want [`least`](http://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.functions.least) and [`greatest`](http://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.functions.greatest) not `min` and `max` – pault Feb 27 '20 at 22:22
  • It throws me an error: TypeError: Invalid argument, not a string or column: 1 of type . For column literals, use 'lit', 'array', 'struct' or 'create_map' function When I use least and greatest – Nick Ryan Feb 27 '20 at 22:54
  • `df.withColumn('coln',(least(greatest(df["cola"],lit(2)),lit(6))))` (where `least`, `greatest` and `lit` come from `pyspark.sql.functions`) – pault Feb 27 '20 at 22:54
  • Thanks a ton....it worked – Nick Ryan Feb 27 '20 at 23:00

0 Answers0