Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

np.select with multiple conditions

conditions = [
    df['gender'].eq('male') & df['pet1'].eq(df['pet2']),
    df['gender'].eq('female') & df['pet1'].isin(['cat', 'dog'])
]

choices = [5,5]

df['points'] = np.select(conditions, choices, default=0)

print(df)
     gender      pet1      pet2  points
0      male       dog       dog       5
1      male       cat       cat       5
2      male       dog       cat       0
3    female       cat  squirrel       5
4    female       dog       dog       5
5    female  squirrel       cat       0
6  squirrel       dog       cat       0
Comment

PREVIOUS NEXT
Code Example
Python :: py foreach 
Python :: how to give a role permissions discord py 
Python :: flask login 
Python :: python dunder 
Python :: pandas rename column by dictionary 
Python :: Iterate string 2 characters at a time in python 
Python :: install pythonjsonlogger 
Python :: pandas get group 
Python :: how to send file in python request 
Python :: string slices 
Python :: numpy array unique value counts 
Python :: how to add a value to a list in python 
Python :: remove space from string python 
Python :: how to create an array in python 
Python :: python json string indices must be integers 
Python :: infinite while python 
Python :: how to change data type from int to float in dataframe 
Python :: fibonacci 
Python :: maximum and minimum value of array python 
Python :: pd dataframe single column rename 
Python :: python square a number 
Python :: numpy random for string 
Python :: python dictionary get keys and values 
Python :: cli args python 
Python :: deleting in a text file in python 
Python :: find value in dictionary python 
Python :: loading in pyqt5 
Python :: depth first search python recursive 
Python :: random.sample python 
Python :: python max function with lambda 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =