Select to view content in your preferred language

Excluding the value 0 with the Python function MinimumValue

1936
3
Jump to solution
01-03-2024 05:16 AM
RudyJamet
Emerging Contributor

Trying to get the minimum value of a list of numeric records excluding the value 0. I found an example in StackExchange that looks something like this:

fieldX = min (filter (not_0, [!field1!, !field2!, !field3!, !field!]))

Code:

def not_0 (value):

      Return value > 0

Tried in Pro, like this:

fieldX = MinimumValue(filter(not_0, [!field1!, !field2!, !field3!, !field!]))

Code:

def not_0 (value):

       Return value > 0

But it doesn’t work because in the code block it’s an “invalid expression. No function was found with the name ‘MinimumValue’”.

I am not sure if it is because of the function filter or the script itself that causes the error.

Any idea on how to exclude the value 0 while still getting the minimum value? Thanks.

 

 

0 Kudos
1 Solution

Accepted Solutions
CodyPatterson
Frequent Contributor

Hey @RudyJamet 

I was curious if you've attempted the use of min() as well? I believe that MinimumValue is a custom function that is not recognized in Python as it uses min() instead. This has worked with my testing using fieldX = min (filter (not_0, [!field1!, !field2!, !field3!, !field!])), if you're comfortable with lambda functions as well, you could attempt this too, which would eliminate the need to use not_0: min(filter(lambda x: x > 0, [!field1!, !field2!, !field3!, !field!])).

If this doesn't work, let me know and I can check it out!

Cody

View solution in original post

3 Replies
CodyPatterson
Frequent Contributor

Hey @RudyJamet 

I was curious if you've attempted the use of min() as well? I believe that MinimumValue is a custom function that is not recognized in Python as it uses min() instead. This has worked with my testing using fieldX = min (filter (not_0, [!field1!, !field2!, !field3!, !field!])), if you're comfortable with lambda functions as well, you could attempt this too, which would eliminate the need to use not_0: min(filter(lambda x: x > 0, [!field1!, !field2!, !field3!, !field!])).

If this doesn't work, let me know and I can check it out!

Cody

RudyJamet
Emerging Contributor

I used the lambda function in the expression and it worked perfectly.

Thanks Cody, and happy new year!

0 Kudos
CodyPatterson
Frequent Contributor

Great to hear, glad it helped! Happy New Year!

0 Kudos