How to Process CSV Data in Python with a Data Helper
Build a beginner-friendly data helper in Python that loads a CSV file, filters rows by a condition, and summarizes numeric fields.
Python code
36 linesimport csv
from pathlib import Path
DATA = [
{"name": "Alice", "score": 88, "passed": True},
{"name": "Bob", "score": 42, "passed": False},
{"name": "Carol", "score": 95, "passed": True},
]
def load_csv(file_path: Path) -> list[dict]:
with file_path.open(newline="", encoding="utf-8") as f:
return list(csv.DictReader(f))
def filter_passed(records: list[dict]) -> list[dict]:
return [r for r in records if r.get("passed") == "True"]
def summarize(records: list[dict]) -> dict:
scores = [int(r["score"]) for r in records]
return {"count": len(scores), "avg_score": round(sum(scores) / len(scores), 2)}
if __name__ == "__main__":
tmp = Path("students.csv")
with tmp.open("w", encoding="utf-8", newline="") as f:
writer = csv.DictWriter(f, fieldnames=["name", "score", "passed"])
writer.writeheader()
writer.writerows(DATA)
students = load_csv(tmp)
passed = filter_passed(students)
print("All records:", summarize(students))
print("Passed records:", summarize(passed))
tmp.unlink()
Output
All records: {'count': 3, 'avg_score': 75.0}
Passed records: {'count': 2, 'avg_score': 91.5}
How it works
The csv.DictReader converts each CSV row into a dictionary keyed by the header names, making field access readable. The filter_passed function uses a list comprehension with .get() to safely check the passed column, comparing against the string 'True' since CSV stores everything as text. The summarize function converts score strings to integers and computes the average, rounding to two decimal places. The if __name__ == "__main__" guard keeps the test data creation and output separate from the reusable helper functions.
Common mistakes
- Forgetting that CSV values are strings; compare against 'True' not True
- Hardcoding file paths instead of using Path and context managers
- Not handling empty rows or missing keys with .get()
- Mixing functions with side effects and pure logic in the same block
Variations
- Use pandas.read_csv for larger datasets and built-in filtering
- Return a generator from load_csv to stream rows instead of loading all at once
Real-world use cases
- ETL jobs that ingest flat files, filter valid entries, and produce aggregate stats for dashboards.
- Batch processing of survey or log exports where only rows meeting a status flag are analyzed.
- Building a lightweight reporting script that summarizes weekly sales data without a full database.
Sponsored
More from Data pipelines & processing
- Add a UUID Surrogate Key to Each Row in a CSV with Python easy
- Attach Source File Metadata to Records in Python easy
- Build a Python Utility That Detects Duplicate Records Across Multiple Excel Sheets medium
- Check Null Rate Threshold in PySpark DataFrame medium
- Count Records Processed per Category in Python easy
- Create Data Helper Functions in Python for Beginners easy
Keep learning
Related tutorials and quizzes for this topic.