I have a table with 3 safe pressure options and a refusal pressure (constant) as such:
|SafeOpt1|SafeOpt2|SafeOpt3|RefP|
---------------------------------
|0 |0 |37 |23 |
|0 |0 |0 |23 |
|0 |25 |0 |23 |
|0 |0 |0 |23 |
And i need to return values with this logic: First non zero value in option 1, if all option 1's are 0 then first non zero in option 2, if all option 2's are 0 then first non zero in option 3, if all option 3's are 0 then take the refusal pressure.
So in the above scenario it would return 25. I have the following code that numerates through one column:
SafeP = next((i for i, x in enumerate(Output.SafeOpt1) if x), Output.refusalpressure)
But I need to chain them together and have been unsuccessful.
Use np.ravel and np.nonzero
In [407]: x = df.values.ravel('K')
In [408]: x[np.nonzero(x)][0]
Out[408]: 25
In [409]: x[np.nonzero(x)]
Out[409]: array([25, 37, 23, 23, 23, 23], dtype=int64)
Or
In [412]: x[x != 0][0]
Out[412]: 25
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With