samedi 24 août 2019

How to program optimally with multiple conditions in pandas

Hi I have a problem with below scenario. How can i optimally program with multiple conditions in pandas

        Col A                   Col B                  Col C

        Paper/Bag/Plastic/      1-100                  <=20
        Rubber/Aluminium        101-300                10 to 20
                                301-400                20 to 40
                                401-500                5 to 90
                                501-600                100 to 120
                                601-700                20 to 50
                                701-800                45 to 60
                                800-900                60 to 80



         Wood/Tin/Green          1-4                    <=10
                                 5-50                   20 to 90
                                 51-60                  4 to 50
                                 61-67                  60 to 80


         Clay/Red/Mat            NA                     6 to 72

         ......

And so on,

when Col-A is equal to certain values, col-B should range in that for which the corresponding range should match in col-C, else its a fail condition otherwise its pass

I have used if condition but it is exhaustive and not getting right result

def test_rule_func(x):
    if(x.loc['col-A'] in ['Paper','Bag','Plastic','Rubber','Alluminium']): 
       if(x.loc['col-B'] >= 1) and (x.loc['col-B'] <= 100) and (x.loc['col-C'] <= 20):
          return 'Pass'
       else:
          return 'Fail'


       if(x.loc['col-B'] >= 101) and (x.loc['col-B'] <= 150) and (x.loc['col-C'] >= 10 and x.loc['col-C'] <=25):
          return 'Pass'
       else:
          return 'Fail'


       if(x.loc['col-B'] >= 151) and (x.loc['col-B'] <= 400) and (x.loc['col=C'] >= 20 and x.loc['col-C'] <=45):
          return 'Pass'
       else:
          return 'Fail'



    elif (x.loc['col-A'] in ['Wood','Tin','Green']): 
       if(x.loc['col-B'] >= 1) and (x.loc['col-B'] <= 90) and (x.loc['col-C'] <= 10):
          return 'Pass'
       else:
          return 'Fail'


       if(x.loc['col-B'] >= 91) and (x.loc['col-B'] <= 250) and (x.loc['col-C'] >= 10 and x.loc['col-C'] <= 25):
          return 'Pass'
       else:
          return 'Fail'




     else:
         print("Logic not present")

Aucun commentaire:

Enregistrer un commentaire