0

I faced a curious doubt when i was executing a script using python.

I have a column called 'Status' which has value as either 'Success' or 'Failure'. I wanted to create two more column called 'SuccessCount' with value '1' if status is 'Success' and 'Failurecount' with value '1' if status is 'Failure'. I thought this was easy if I use lambda function and used below code

dffil["SuccessCount"] = dffil["Status"].apply(lambda x: 1 if x is 'Success' else 0)
dffil["FailureCount"] = dffil["Status"].apply(lambda x: 1 if x is 'Failure' else 0)

Now look at the output it generated

enter image description here

It gave SuccessCount as '0' even if the status was 'Success'. I got many values with such scenario. I checked for whitespace, removed it still the output didn't change.

Then I executed below code

dffil["SuccessCount"] = dffil["Status"].apply(lambda x: 1 if ((x=='Success') is True) else 0)
dffil["FailureCount"] = dffil["Status"].apply(lambda x: 1 if ((x=='Failure') is True) else 0)

This time it gave proper output, with a small change in lambda function, but logic was same

enter image description here

I am really curious to know what was the issue in first lambda funtion

SoKu
  • 117
  • 6
  • 1
    TL;DR: you very rarely want to use `is`. The correct comparison is simply `1 if x == 'Success' else 0`, or `int(x == 'Success')` for short. – deceze Nov 24 '21 at 07:57

0 Answers0