Prepare Recipe Formula

Options
bhakuniv
bhakuniv Registered Posts: 9 ✭✭✭✭

Hi

I am trying to extract a Column using 6 related columns, the output column will the input column with the highest associated score if atleast one of the input columns has a value other than (Undefined or Unknown)

Please refer to the attached excel (Col4 & Val4 are the output columns), is there any way to do this other than the formula step in prepare recipe

Thanks

VB

Best Answer

  • MickaelH
    MickaelH Dataiker, Registered Posts: 31 Dataiker
    Answer ✓
    Options

    Hi,

    Since you need to:

    • define some "complex" rules to extract the values
    • generate 2 columns

    I think it's the easiest and fastest way to do that.

    If I understood correctly your use case but you won't be able to translate that into simpler processors. Maybe you can share the corresponding formula used in the spreadsheet that you used for the screenshot?

    To illustrate the "python function" that could be used to solve your problem here is an example code snippet:

    def process(row):pairs = [['Col1', 'Val1'], ['Col2', 'Val2'], ['Col3', 'Val3']]max_val = Nonemax_col = Nonemax_val_undef = None # Used when all the column names are undefined/unknownfor column_column_name, value_column_name in pairs:current_column = get_column_name(column_column_name, row)current_value = float(row.get(value_column_name, '0'))if current_column is None:max_val_undef = max(current_value, max_val_undef)else:if max_val is None or current_value > max_val:max_val = current_valuemax_col = current_columnif max_col is None:computed_column = ''computed_value = max_val_undefelse:computed_column = max_colcomputed_value = max_valrow['Col4'] = computed_columnrow['Val4'] = computed_valuereturn rowdef get_column_name(column_column_name, row):result = row.get(column_column_name, None)if result.lower() in ['undefined', 'unknown']:return Nonereturn result

    I hope this helps.

Answers

  • MickaelH
    MickaelH Dataiker, Registered Posts: 31 Dataiker
    Options

    Hi,

    Just to be sure I understood your use case. The input dataset will look like the one in the attached screenshot ?By that I mean that it will be made of the 6 columns and you need to both check for the first defined "ColN" value. If there are multiple "ColN" values, you then compare the "ValN" value and the N that needs to be kept is the one with the greater "ValN" ? (and another particular case where all the "ColN" are undefined/unknown")

    You want to produce 2 new columns that will contains for each line the "ColN" and "ValN" values for the computed "N" ?

  • bhakuniv
    bhakuniv Registered Posts: 9 ✭✭✭✭
    Options

    Hi Michael

    The input dataset will be the first 6 columns always no need to check on the count there

    the output should be the column and the corresponding value( where the value is the highest) however if any of the columns is undefined or unknown it should be ignored unless all 3 columns are undefined or unknown

    the expected output is highlighted in the attachment for each row

  • MickaelH
    MickaelH Dataiker, Registered Posts: 31 Dataiker
    Options

    So to do that, you could use a "Python function" step in a "Prepare recipe".

    You must use the mode "row: return a row for each row".

    This way you will able to write all your logic to compute the "Col4" and "Val4" values.

    You can then add another step in your prepare recipe to remove all the other columns if they are not useful.

  • bhakuniv
    bhakuniv Registered Posts: 9 ✭✭✭✭
    Options

    Thanks Mickael, I assume there is no other way to get this done other than a custom formula

Setup Info
    Tags
      Help me…