Data Wrangling with Python Datatable - Conditional Statements#
Transformation based on a Single Condition#
Task: if c is positive, then value should be a - b, else b - a.
from datatable import dt, f, update, ifelse
df = dt.Frame({"a": [1, 1, 2, 4], "b": [2, 2, 3, 2], "c": [3, -3, 2, -1]})
df
| a | b | c | |
|---|---|---|---|
| ▪▪▪▪ | ▪▪▪▪ | ▪▪▪▪ | |
| 0 | 1 | 2 | 3 |
| 1 | 1 | 2 | −3 |
| 2 | 2 | 3 | 2 |
| 3 | 4 | 2 | −1 |
The ifelse function handles conditional transformations. It is similar to python’s if…else statements or SQL’s case_when function.
BREAKDOWN OF SOLUTION
Step 1 : Define the condition, with the
TrueandFalsevalues.
condition = f.c >= 0 # positive values
true = f.a - f.b
false = f.b - f.a
Step 2 : Create the ifelse expression.
if_statement = ifelse(condition, true, false)
Step 3: Apply the
if_statementto the datatable frame to get the results
df[:, update(b=if_statement)]
df
| a | b | c | |
|---|---|---|---|
| ▪▪▪▪ | ▪▪▪▪ | ▪▪▪▪ | |
| 0 | 1 | −1 | 3 |
| 1 | 1 | 1 | −3 |
| 2 | 2 | −1 | 2 |
| 3 | 4 | −2 | −1 |
Transformation based on Multiple Conditions#
df = dt.Frame({"Type": ["A", "B", "B", "C"], "Set": ["Z", "Z", "X", "Y"]})
df
| Type | Set | |
|---|---|---|
| ▪▪▪▪ | ▪▪▪▪ | |
| 0 | A | Z |
| 1 | B | Z |
| 2 | B | X |
| 3 | C | Y |
Task:
if
Setis equal to ‘Z’ andTypeis equal to ‘A’ then assign ‘yellow’ tocolor.If
Setis equal to ‘Z’ andTypeis equal to ‘B’ then assign ‘blue’ tocolor.If
Typeis equal to ‘B’ then assign ‘purple’ tocolor.Otherwise, assign ‘black’ to
color.
BREAKDOWN OF SOLUTION
Step 1 : Define the conditions, with the
TrueandFalsevalues.
condition1 = (f.Set == "Z") & (f.Type == "A")
true1 = "yellow"
condition2 = (f.Set == "Z") & (f.Type == "B")
true2 = "blue"
condition3 = f.Type == "B"
true3 = "purple"
false = "black"
Step 2 : Create the ifelse expression.
if_statements = ifelse(condition1, true1, condition2, true2, condition3, true3, false)
Step 3: Apply the
if_statementsto the datatable frame to get the results
df[:, update(color=if_statements)]
df
| Type | Set | color | |
|---|---|---|---|
| ▪▪▪▪ | ▪▪▪▪ | ▪▪▪▪ | |
| 0 | A | Z | yellow |
| 1 | B | Z | blue |
| 2 | B | X | purple |
| 3 | C | Y | black |
Resources:
Comments#