Conditional Logic
General Info
Useful Snippets

10.0.11 CASE WHEN logic - Option 3 -create and apply a custom function

Description:

Use CASE WHEN Logic with custom logic in a function. This option works great when you have custom logic, or logic that you want to reuse multiple times

Example:

# Create a function that applies the same if/then logic as above
def CASE_WHEN_func(column):
    return_value = None

    if column == 'b':
        return_value = 'value_if_true'

    if column != 'b':
        return_value = 'value_if_false'
    return(return_value)

# Create a new column by applying the function
DF['D'] = DF['B'].apply(CASE_WHEN_func)