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.
Python code
27 linesimport 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
{'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
- Use `csv.reader` and `enumerate` to filter by index instead of headers.
- 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
More from Files & data
- Append a Line to a Log File in Python easy
- Audit File Permissions Across a Project in Python easy
- Automatically Detect Corrupted Files Using SHA-256 Checksums in Python easy
- Automatically Highlight Data Validation Errors Inside Excel Files in Python easy
- Build a Command-Line To-Do List Application with Data Persistence in Python easy
- Build a File Index by Relative Path Hash Map in Python easy
Keep learning
Related tutorials and quizzes for this topic.