Answers for "apply multiple conditions to pandas dataframe"

8

filter dataframe multiple conditions

# when you wrap conditions in parantheses, you give order
# you do those in brackets first before 'and'
# AND
movies[(movies.duration >= 200) & (movies.genre == 'Drama')]
Posted by: Guest on March-21-2020
0

pandas set condition multi columns

conditions = [
            (df['col2'] == 'Z') & (df['col1'] == 'A'),
            (df['col2'] == 'Z') & (df['col1'] == 'B'),
            (df['col1'] == 'B')
			]


choices = ['yellow', 'blue', 'purple']

df['color'] = np.select(conditions, choices, default='black')

df
# Out[216]: 
#  col1 col2   color
# 0    A    Z  yellow
# 1    B    Z    blue
# 2    B    X  purple
# 3    C    Y   black
Posted by: Guest on April-19-2022

Code answers related to "apply multiple conditions to pandas dataframe"

Python Answers by Framework

Browse Popular Code Answers by Language