person health life power nexy prox
mike 40 50 45 20 40
Anton 20 30 10 50 40
I want to create a column four_of_five
that return yes
if there's a value greater than 40 in at least 4 of these 5 columns: health, life, power, nexy, prox
person health life power nexy prox four_of_five
mike 40 50 45 20 40 yes
Anton 20 30 10 50 40 no
mike return yes because he has values equal or greater than 40 in 4 out of 5 columns Anton return no because he has values equal or greater than 40 in only 2 out of 5 columns
I want to solve this without using a lot of if's and or's
. Is there an easy way to do it?
CodePudding user response:
here is one way to do it without if
df['four_of_five']=(
((df.iloc[:,1:]>=40) # True/False where value meets or exceeds 40
.sum(axis=1)>=4) # count across the row and check if its equal or exceeds 4
.map({True: 'Yes', False:'No'})) # map True/False to Yes, No
df
person health life power nexy prox four_of_five
0 mike 40 50 45 20 40 Yes
1 Anton 20 30 10 50 40 No