Python CSV & Data Analysis
In this page:
Reading CSV Files
The built-in csv module reads comma-separated data row by row without needing pandas as a dependency. This tutorial uses io.StringIO to simulate an in-memory CSV file, so the examples run without needing an actual file on disk -- in real code you'd typically open a real file and pass its handle instead.
Example: Reading CSV Files
import csv
import io
text = "name,age\nAlex,30\nSam,25\n"
reader = csv.reader(io.StringIO(text))
for row in reader:
print(row)
Writing CSV Files
csv.writer writes individual rows as plain lists of values, while csv.DictWriter writes rows from dictionaries and requires you to specify the column headers (fieldnames) up front. DictWriter is usually clearer when your data already exists as a list of dictionaries rather than parallel lists.
Example: Writing CSV Files
import csv
import io
buffer = io.StringIO()
writer = csv.DictWriter(buffer, fieldnames=["name", "age"])
writer.writeheader()
writer.writerow({"name": "Alex", "age": 30})
print(buffer.getvalue())
CSV Data Filtering
Filtering CSV data means iterating over the parsed rows and checking a condition on each one, keeping only the rows that pass -- the same pattern as filtering any other list of records, just applied to rows read from a file rather than data already in memory.
Example: CSV Data Filtering
import csv
import io
text = "name,age\nAlex,30\nSam,17\n"
reader = csv.DictReader(io.StringIO(text))
adults = [row for row in reader if int(row["age"]) >= 18]
print(adults)
Aggregating CSV Metrics
Once rows are parsed into numbers (remember: csv values arrive as strings and need explicit conversion), you can compute sums, averages, or other aggregate metrics with ordinary Python loops or the statistics module, the same way you would for any other numeric dataset.
Example: Aggregating CSV Metrics
import csv
import io
text = "name,score\nAlex,80\nSam,90\n"
reader = csv.DictReader(io.StringIO(text))
scores = [int(row["score"]) for row in reader]
print(sum(scores) / len(scores))
Reading CSV with Pandas
pandas.read_csv() loads an entire CSV file directly into a DataFrame in one call, handling type inference, header detection, and missing values automatically. For anything beyond very simple parsing, this is far less code than manually working with the csv module row by row.
Example: Reading CSV with Pandas
import pandas as pd
import io
text = "name,age\nAlex,30\nSam,25\n"
df = pd.read_csv(io.StringIO(text))
print(df)
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Python NumPy Introduction
- Python NumPy Arrays
- Python Pandas Introduction
- Python Pandas DataFrame
- Python Matplotlib Basics
- Python Data Visualization
- Python Statistics Module
- Python CSV & Data Analysis
- Python requests Module
- Python JSON & APIs
- Python Web Scraping Basics
- Python Flask Introduction
- Python Django Introduction
- Python MongoDB