Dela
Dela

Reputation: 115

Multiple OR conditions using numpy Where

I'm trying to achieve a Column based or certain conditions from other columns and it's basically that, if any of these conditions are met, the results should be a NO in the NEW column, else a YES; how ever i tend to get A huge chunk of errors, even when I use the 'OR' in place of the '|', could anyone help to realize this, as the different approaches I have tried don't seem to work

sales["NEW"]=np.where((sales.Status=='Done' | 
                sales.Status=='out' | 
                sales.Status=='in' | 
                sales.sumPaid>dailyAmount | 
                sales.days<14), 'No','Yes')


sales["NEW"]=np.where((sales.Status==('Done|out |in') | sales.sumPaid>dailyAmount |sales.days<14), 'No','Yes')

TypeError Traceback (most recent call last) C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\ops\array_ops.py in na_logical_op(x, y, op) 273 # (xint or xbool) and (yint or bool) --> 274 result = op(x, y) 275 except TypeError:

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\ops\roperator.py in ror_(left, right) 55 def ror_(left, right): ---> 56 return operator.or_(right, left) 57

TypeError: unsupported operand type(s) for |: 'str' and 'str'

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last) C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\ops\array_ops.py in na_logical_op(x, y, op) 287 try: --> 288 result = libops.scalar_binop(x, y, op) 289 except (

pandas_libs\ops.pyx in pandas._libs.ops.scalar_binop()

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\ops\roperator.py in ror_(left, right) 55 def ror_(left, right): ---> 56 return operator.or_(right, left) 57

TypeError: unsupported operand type(s) for |: 'bool' and 'str'

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last) in 212 213 sales["NEW"]=np.where((sales.Status=='Done' | --> 214 sales.Status=='out' | 215 sales.Status=='in' | 216 sales.sumPaid>dailyAmount |

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\ops\common.py in new_method(self, other) 62 other = item_from_zerodim(other) 63 ---> 64 return method(self, other) 65 66 return new_method

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\ops_init_.py in wrapper(self, other) 547 rvalues = extract_array(other, extract_numpy=True) 548 --> 549 res_values = logical_op(lvalues, rvalues, op) 550 return _construct_result(self, res_values, index=self.index, name=res_name) 551

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\ops\array_ops.py in logical_op(left, right, op) 364 filler = fill_int if is_self_int_dtype and is_other_int_dtype else fill_bool 365 --> 366 res_values = na_logical_op(lvalues, rvalues, op) 367 res_values = filler(res_values) # type: ignore 368

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\ops\array_ops.py in na_logical_op(x, y, op) 296 typ = type(y).name 297 raise TypeError( --> 298 f"Cannot perform '{op.name}' with a dtyped [{x.dtype}] array " 299 f"and scalar of type [{typ}]" 300 )

TypeError: Cannot perform 'ror_' with a dtyped [object] array and scalar of type [bool]

Upvotes: 1

Views: 318

Answers (2)

srishtigarg
srishtigarg

Reputation: 1204

The second way that you are mentioning is incorrect, it will not work by putting | inside strings.

The first way can be worked out by putting parentheses:

sales["NEW"]=np.where(((sales.Status=='Done') | 
                (sales.Status=='out') | 
                (sales.Status=='in') | 
                (sales.sumPaid>dailyAmount) | 
                (sales.days<14)), 'No','Yes')

Upvotes: 2

Parfait
Parfait

Reputation: 107587

To combine consider DataFrame.isin where each condition is enclosed in paired parentheses. Below accesses columns by [] and uses functional form of mathematical operators such as Series.gt and Series.lt:

sales["NEW"] = np.where((sales["Status"].isin(['Done','out','in'])) | 
                        (sales["sumPaid"].gt(dailyAmount)) |
                        (sales["days"].lt(14)), 
                        'No',
                        'Yes')

Upvotes: 2

Related Questions