Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

pandas change column value based on multiple condition

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

choices = [5, 2]

df['points'] = np.select(conditions, choices, default=0)
 
PREVIOUS NEXT
Tagged: #pandas #change #column #based #multiple #condition
ADD COMMENT
Topic
Name
5+3 =