-1

I have a table with the following information

No of transactions GST Item cost GST inclusive
1 0 0.5

I wish to include an additonal column with the following logic

def incl(s):
    if ((s['No of Transactions'] ==0) & (s['GST'] ==0)):
        return s['Item Cost GST inclusive']
    else:
        return s['Item Cost GST inclusive']*0.9

df2['Item Cost GST exclusive'] = df2.apply(incl, axis=1)  

I am getting the following table

No of transactions GST Item cost GST inclusive Item cost GST exclusive
1 0 0.5 0.45

Logically when there is no GST, item cost GST inclusive does not get multlplied by 0.9, because there is no point in triggering the condition. Also generally in cases where 'no of transaction' is 0, there is no item cost at all.

I was expecting the following table

No of transactions GST Item cost GST inclusive Item cost GST exclusive
1 0 0.5 0.5

I am not sure what went wrong in the logic. I am sure the logic is wrong. Please help.

I will appreciate any kind suggestions please.

Mihan
  • 189
  • 2
  • 15
  • 1
    In Python use `and` not `&` for Boolean logic. – Mark Jun 17 '22 at 06:37
  • Thank you so much...Is it true for or as well? – Mihan Jun 17 '22 at 06:42
  • 3
    Because `'No of transactions'` is not 0 but `'GST'` is 0, it does not meet the if condition, so you get 0.45. Is there a problem? – Mechanic Pig Jun 17 '22 at 06:44
  • @Thierry That's not the issue. – Kelly Bundy Jun 17 '22 at 06:48
  • Logically when there is no GST, item cost GST exclusive does not get multlplied by 0.9, because there is no point in triggering the condition. Also generally in cases where no of transaction is 0, there is no item cost at all. What is the mistake in the logic guys? – Mihan Jun 17 '22 at 07:04
  • 1
    @Mechanic pig, I am slightly confused. I think or will not do the trick and 'and' is taking the first condition only. The correct condition Thretically, fisrt look into no if transaction , and if it is not 0 than look into GST to trigger the calculation. – Mihan Jun 17 '22 at 07:10
  • @Mihan I think what you need is `or` instead of `and`. The former only requires one of the two conditions to be true, and the latter requires both to be true. – Mechanic Pig Jun 17 '22 at 07:12

1 Answers1

0
def incl(s):
    if ((s['No of Transactions'] ==0) or (s['GST'] ==0)):
        return s['Item Cost GST inclusive']
    else:
        return s['Item Cost GST inclusive']*0.9

df2['Item Cost GST exclusive'] = df2.apply(incl, axis=1)  

This should do it.

rohith santosh
  • 28
  • 6
  • 23