Loading...

verticapy.sql.functions.case_when#

verticapy.sql.functions.case_when(*args) StringSQL#

Returns the conditional statement of the input arguments.

Parameters#

args: SQLExpression

Infinite number of Expressions. The expression generated will look like:

even:

CASE … WHEN args[2 * i] THEN args[2 * i + 1] … END

odd :

CASE … WHEN args[2 * i] THEN args[2 * i + 1] … ELSE args[n] END

Returns#

StringSQL

SQL string.

Examples#

First, let’s import the vDataFrame in order to create a dummy dataset.

from verticapy import vDataFrame

Now, let’s import the VerticaPy SQL functions.

import verticapy.sql.functions as vpf

We can now build a dummy dataset.

df = vDataFrame({"x": [0.8, -1, 0, -2, 0.5]})

Now, let’s go ahead and apply the function.

df["x_pos"] = vpf.case_when(
    df["x"] > 0, 1,
    df["x"] == 0, 0,
    -1,
)
display(df)
123
x
Numeric(21)
100%
123
x_pos
Integer
100%
10.81
2-1.0-1
30.00
4-2.0-1
50.51

Note

It’s crucial to utilize VerticaPy SQL functions in coding, as they can be updated over time with new syntax. While SQL functions typically remain stable, they may vary across platforms or versions. VerticaPy effectively manages these changes, a task not achievable with pure SQL.

See also

vDataFrame.eval() : Evaluates the expression.