How to use all() and any() function in pandas with multiple columns

Viewed 70

I need one help regarding: How to use all and any function in Pandas with multiple columns. Below is my data frame:

               ResolutionCodeMapID  CauseCodeMapID  TicketTypeMapID  multiple
ApplicationID                                                                
129272                          61              12               48      True

image

Here I want True in multiple columns, if all column values are greater than 60. If all col values are greater than 30 also, I need as True. If any one column value is less than 10, then it should be False.

I'v tried the code below, but it's not giving the expected answer:

df["multiple"] = (df[cols]>=50).all(axis="columns") 
1 Answers

Solution generate True if all values are greater or equal like 10 with DataFrame.all:

cols = ['CauseCodeMapID','CauseCodeMapID','TicketTypeMapID']

df["multiple"] = df["multiple"] = (df[cols]>=10).all(axis=1)
print (df)
        ResolutionCodeMapID  CauseCodeMapID  TicketTypeMapID  multiple
129272                   61              12               48      True
129272                   61              12                8     False

Or:

df["multiple"] = np.where((df[cols]<10).any(axis=1), False, True)
Related