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')]
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')]
how to filter pandas dataframe column with multiple values
# Multiple Criteria dataframe filtering
movies[movies.duration >= 200]
# 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')]
# OR
movies[(movies.duration >= 200) | (movies.genre == 'Drama')]
(movies.duration >= 200) | (movies.genre == 'Drama')
(movies.duration >= 200) & (movies.genre == 'Drama')
# slow method
movies[(movies.genre == 'Crime') | (movies.genre == 'Drama') | (movies.genre == 'Action')]
# fast method
filter_list = ['Crime', 'Drama', 'Action']
movies[movies.genre.isin(filter_list)]
filter dataframe by two columns
#tl;dr use the '&' opperator and not the word 'and'
print(df)
OUTPUT
Name Age
0 Joe 23
1 Tom 54
2 Joe 34
3 Tom 42
4 Joe 23
5 Tom 54
6 Joe 34
7 Tom 42
tom_and_42 = df[(df["Name"]=="Tom") & (df["Age"]==42)]
print(tom_and_42)
OUTPUT
Name Age
3 Tom 42
7 Tom 42
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us