0

This is my actual requirement, and i tried to replicate in python by using either np.where(numpy) or logical operator(logical_and). but i got syntax error.

please help me to resolve the issues

Actual logic:

IF (AND ([Key] <> "", MATCHES ([Comments], "")), "To Be Assesed", IF ([Aging] = 0, "To Be Assesed", [Comments]))

Python Code:

logical_and:

Final_Output['Comments']= np.logical_and(Final_Output['Key']<>"",Final_Output['Comments']==""),"ToBeAssesed",np.where(Final_Output['Aging']=0,"ToBeAssesed",Final_Output['Comments'])

np.where:

Final_Output['Comments']= np.where((Final_Output['Key']<>'' , Final_Output['Comments']=''),'ToBeAssesed',np.where(Final_Output['Aging']=0,'ToBeAssesed',Final_Output['Comments']))

Thanks, Sabarinathan.C

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • What is the syntax error you are receiving? Please post the full traceback. – nomansland008 Jul 07 '20 at 20:06
  • is it the use of `<>` instead of `!=`? – Dr. V Jul 07 '20 at 20:10
  • File "", line 46 Final_Output['Comments']= np.where((Final_Output['Key']!='',Final_Output['Comments']=''),"ToBeAssesed",np.where(Final_Output['Aging']=0,"ToBeAssesed",Final_Output['Comments'])) ^ SyntaxError: invalid syntax – Sabarinathan Jul 07 '20 at 20:19

1 Answers1

0

There are multiple things mixed up and confused.

First, I advice to carefully read the numpy where specs. You will find, there is no possibility to pass only two arguments to numpy.where.

Second, you seem to compare empty / non-empty stings. In python an empty string results in boolean false. You can read about it here.

your definition of

Final_Output['Key']<>'' , Final_Output['Comments']=''

should read as

Final_Output['Key'], not Final_Output['Comments']

That even conforms to python's(3) syntax.

Third, mind the equal operations:

= is an assignment from right to left and it is a statement, not an expression. It therefore cannot be used in composed expressions. It will result in a syntax error

== is for testing equality between left and right and results in a boolen

Fourth, the diamond operator <> is dangerous (testing for inequality). Whether it works, depends on the version of python in use. In python 2 it does what you expect. But in python 3 it is not there any more and its use results in a syntax error. So you'd be better off using the statement given above (under "Second").

Mind that python 2 is no more supported. Future dev should be done with python 3.

woodz
  • 737
  • 6
  • 13