🚀
Pandas
02 Conditional Filtering
++++
Data Science
May 2026×Notebook lesson

Notebook converted from Jupyter for blog publishing.

02-Conditional-Filtering

Driptanil Datta
Driptanil DattaSoftware Developer

Conditional Filtering

Imports

import numpy as np
import pandas as pd
df = pd.read_csv('tips.csv')
df.head()
HTML
MORE
total_bill
tip
sex
smoker
day

Conditions

# df['total_bill'] > 30
bool_series = df['total_bill'] > 30
df[bool_series]
HTML
MORE
total_bill
tip
sex
smoker
day
df[df['total_bill']>30]
HTML
MORE
total_bill
tip
sex
smoker
day
df[df['sex'] == 'Male']
HTML
MORE
total_bill
tip
sex
smoker
day

Multiple Conditions

Recall the steps:

  • Get the conditions
  • Wrap each condition in parenthesis
  • Use the | or & operator, depending if you want an
    • OR | (either condition is True)
    • AND & (both conditions must be True)
  • You can also use the ~ operator as a NOT operation
df[(df['total_bill'] > 30) & (df['sex']=='Male')]
HTML
MORE
total_bill
tip
sex
smoker
day
df[(df['total_bill'] > 30) & ~(df['sex']=='Male')]
HTML
MORE
total_bill
tip
sex
smoker
day
df[(df['total_bill'] > 30) & (df['sex']!='Male')]
HTML
MORE
total_bill
tip
sex
smoker
day
# The Weekend
df[(df['day'] =='Sun') | (df['day']=='Sat')]
HTML
MORE
total_bill
tip
sex
smoker
day

Conditional Operator isin()

We can use .isin() operator to filter by a list of options.

options = ['Sat','Sun']
df['day'].isin(options)
RESULT
MORE
0       True
1       True
2       True
3       True
4       True
df[df['day'].isin(['Sat','Sun'])]
HTML
MORE
total_bill
tip
sex
smoker
day
Drip

Driptanil Datta

Software Developer

Building full-stack systems, one commit at a time. This blog is a centralized learning archive for developers.

Legal Notes
Disclaimer

The content provided on this blog is for educational and informational purposes only. While I strive for accuracy, all information is provided "as is" without any warranties of completeness, reliability, or accuracy. Any action you take upon the information found on this website is strictly at your own risk.

Copyright & IP

Certain technical content, interview questions, and datasets are curated from external educational sources to provide a centralized learning resource. Respect for original authorship is maintained; no copyright infringement is intended. All trademarks, logos, and brand names are the property of their respective owners.

System Operational

© 2026 Driptanil Datta. All rights reserved.