3

I have the following data frame:

+----+----+----+----+
|col0|col1|col2|col3|
+----+----+----+----+
|   1|  21|   3|null|
|   4|   5|  23|null|
|null|   4|   5|   6|
|null|   9|  22|  42|
+----+----+----+----+

I tried computing the minimum of the column 'col1' and 1.5:

import pyspark.sql.functions as F

cond = df['col2'] > 10
df = df.withColumn('new_col', F.when(cond, F.least(F.col('col1')*0.2, 1.5)).otherwise(F.lit(100)))
df.show()

But I got the following exception:

TypeError: Invalid argument, not a string or column: 1.5 of type <class 'float'>. For column literals, use 'lit', 'array', 'struct' or 'create_map' function.

1 Answer 1

4

Use F.lit(1.5) inside F.least, because it requires a column and does not accept a float:

df2 = df.withColumn('new_col', F.when(cond, F.least(F.col('col1')*0.2, F.lit(1.5))).otherwise(F.lit(100)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.