## Sign up to take part

Registered users can ask their own questions, contribute to discussions, and be part of the Community!

This website uses cookies. By clicking OK, you consent to the use of cookies. Read our cookie policy.

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results forÂ

Registered users can ask their own questions, contribute to discussions, and be part of the Community!

- Community
- Â»
- Discussions
- Â»
- Using Dataiku
- Â»

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Solved!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Prepare Recipe Formula

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

1 Solution

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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 = None
max_col = None
max_val_undef = None # Used when all the column names are undefined/unknown
for 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_value
max_col = current_column
if max_col is None:
computed_column = ''
computed_value = max_val_undef
else:
computed_column = max_col
computed_value = max_val
row['Col4'] = computed_column
row['Val4'] = computed_value
return row
def get_column_name(column_column_name, row):
result = row.get(column_column_name, None)
if result.lower() in ['undefined', 'unknown']:
return None
return result
```

I hope this helps.

MickaÃ«l | R&D at Dataiku

Solutions shown first - Read whole discussion

5 Replies

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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" ?

MickaÃ«l | R&D at Dataiku

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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.

MickaÃ«l | R&D at Dataiku

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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 = None
max_col = None
max_val_undef = None # Used when all the column names are undefined/unknown
for 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_value
max_col = current_column
if max_col is None:
computed_column = ''
computed_value = max_val_undef
else:
computed_column = max_col
computed_value = max_val
row['Col4'] = computed_column
row['Val4'] = computed_value
return row
def get_column_name(column_column_name, row):
result = row.get(column_column_name, None)
if result.lower() in ['undefined', 'unknown']:
return None
return result
```

I hope this helps.

MickaÃ«l | R&D at Dataiku