How to Filter CSV Rows by Column Value in Python

Filter CSV rows based on a column value condition using the standard csv module and a lambda function.

Easy Python 3.9+ Aug 9, 2026 Files & data 18 views 0 copies

Python code

27 lines
Python 3.9+
import csv

def filter_csv(input_file, output_file, column, condition):
    with open(input_file, newline='', encoding='utf-8') as infile, \
         open(output_file, 'w', newline='', encoding='utf-8') as outfile:
        reader = csv.DictReader(infile)
        fieldnames = reader.fieldnames
        writer = csv.DictWriter(outfile, fieldnames=fieldnames)
        writer.writeheader()
        
        for row in reader:
            if condition(row[column]):
                writer.writerow(row)

if __name__ == "__main__":
    input_path = "data.csv"
    output_path = "filtered.csv"
    
    with open(input_path, "w", newline="", encoding="utf-8") as f:
        f.write("name,age,city\nAlice,30,New York\nBob,25,Boston\nCharlie,35,Chicago\nDiana,28,Boston\n")
    
    filter_csv(input_path, output_path, "city", lambda city: city == "Boston")
    
    with open(output_path, newline='', encoding='utf-8') as f:
        reader = csv.DictReader(f)
        for row in reader:
            print(row)

Output

stdout
{'name': 'Bob', 'age': '25', 'city': 'Boston'}
{'name': 'Diana', 'age': '28', 'city': 'Boston'}

How it works

The csv.DictReader reads each row as a dictionary with column headers as keys, making it easy to access the value of a specific column. The condition is a callable that takes the cell value and returns True/False; here we use a lambda to check if the city is 'Boston'. csv.DictWriter writes only the rows that pass the condition, preserving the original column order from the input file via fieldnames. Writing the header first ensures the output CSV remains valid with the same structure.

Common mistakes

  • Forgetting to use `newline=''` when opening CSV files, which causes extra blank lines on Windows.
  • Assuming the column value is always present; use `row.get(column, '')` to avoid KeyError on missing fields.
  • Not converting strings to correct types (e.g., age) before comparison; '30' != 30.
  • Opening the output file in 'w' mode without using `with` statement, risking file handle leaks.

Variations

  1. Use `csv.reader` and `enumerate` to filter by index instead of headers.
  2. Filter in memory with list comprehension: `rows = [row for row in reader if condition(row[column])]`.

Real-world use cases

  • Extracting only high-value customers from a sales export to send targeted promotions.
  • Cleaning dataset rows where a status column equals 'active' before feeding into an ML pipeline.
  • Preparing regional subsets of a global user list for localized reporting or audit.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Files & data

Related tutorials and quizzes for this topic.