Data Filtering

Python · Pandas Basics

Theory

Filtering keeps only the rows that meet a condition, like SQL's WHERE. It works in two steps: df['amount'] > 1000 produces a boolean Series (True or False for every row), and df[mask] keeps the rows where the mask is True, with their original index labels preserved.

Combine conditions with & (and), | (or) and ~ (not), wrapping each condition in parentheses because of operator precedence: df[(df['city'] == 'Pune') & (df['amount'] > 1000)]. Python's and/or do not work here and raise an error. Helpful shortcuts are isin() for lists (df[df['city'].isin(['Pune', 'Delhi'])]), between() for inclusive ranges, str.contains() for text, isna()/notna() for missing values and df.query("city == 'Pune' and amount > 1000") for a SQL-like string. Use df.loc[mask, 'name'] to filter rows and pick columns together.

Why we use it

Analysis questions usually concern a subset - high-value orders, one region, last month's sales. Boolean filtering pulls that subset out in one readable line with no loops.

When to use it

Use boolean masks for row filters on any condition, and query() when a long condition reads better as text. To modify the filtered rows, use df.loc[mask, 'col'] = value rather than chained indexing, which can trigger SettingWithCopyWarning.

How it works

  1. df[(df['city'] == 'Pune') & (df['amount'] > 1000)]
  2. Evaluate each condition for every row
  3. Combine the boolean Series with & / | / ~
  4. Is the combined mask True for this row? Yes: Keep the row. No: Drop the row.
  5. New DataFrame of matching rows

Syntax

df[df['col'] > value]
df[(cond1) & (cond2)]
df[df['col'].isin([...])]
df.query("col > value")

Example

big = df[(df['city'] == 'Pune') & (df['amount'] > 1000)]
print(big)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic