case_when

In [ ]:
case_when(*argv)

Returns the conditional statement of the input arguments.

Parameters

Name Type Optional Description
argv
object
❌
Infinite Number of Expressions.
The expression generated will look like:
even: CASE ... WHEN argv[2 * i] THEN argv[2 * i + 1] ... END
odd : CASE ... WHEN argv[2 * i] THEN argv[2 * i + 1] ... ELSE argv[n] END

Returns

str_sql : SQL expression.

Example

In [69]:
from verticapy import *
df = tablesample({"x": [0.8, -1, 0, -2, 0.5]}).to_vdf()
display(df)
123
x
Numeric(19,1)
10.8
2-1.0
30.0
4-2.0
50.5
Rows: 1-5 | Column: x | Type: numeric(19,1)
In [70]:
# Importing the stats module
import verticapy.stats as st

# Applying the case_when function
df["x_pos"] = st.case_when(df["x"] > 0,  1,
                           df["x"] == 0, 0,
                           -1)
display(df)
123
x
Numeric(19,1)
123
x_pos
Integer
10.81
2-1.0-1
30.00
4-2.0-1
50.51
Rows: 1-5 | Columns: 2