You’ve opened a CSV file in Excel and it crashed because it has 2 million rows. Or you’ve been using csv.reader() and manually handling quoting issues for an hour. Pandas handles both problems in one line — here’s how to use it properly.
In this tutorial, you will learn to:
- Read CSV files into a pandas DataFrame using
read_csv() - Perform instant data analysis without loops — filtering, sorting, aggregation
- Handle different delimiters (tabs, semicolons) with the
sepparameter - Control which rows and columns to load using
usecols,skiprows,nrows - Handle missing values and parse dates automatically
Page Contents
Prerequisites
Basic Python knowledge — familiarity with file handling in Python and Python lists is recommended. No prior pandas experience needed.
What is a CSV File?
A CSV (Comma-Separated Values) file is a plain text file where each line represents a row, and values within a row are separated by commas. It’s the most common format for exchanging tabular data between applications — from Excel exports to database dumps to API responses.
Sample CSV Data
name,score,city
Alice,95,Bangalore
Bob,82,Mumbai
Charlie,91,Chennai
Diana,78,Hyderabad
Ethan,88,Pune
This is how the data looks when visualized as a table:
+----------+-------+-----------+
| name | score | city |
+----------+-------+-----------+
| Alice | 95 | Bangalore |
| Bob | 82 | Mumbai |
| Charlie | 91 | Chennai |
| Diana | 78 | Hyderabad |
| Ethan | 88 | Pune |
+----------+-------+-----------+
Reading CSV with Pandas
The read_csv() function is the workhorse of pandas. It reads the file and returns a DataFrame — a 2D table with labeled rows and columns. Under the hood, pandas uses the Python csv module and efficient C parsers to handle files of any size.
import pandas as pd
from io import StringIO
csv_data = """name,score,city
Alice,95,Bangalore
Bob,82,Mumbai
Charlie,91,Chennai
Diana,78,Hyderabad
Ethan,88,Pune"""
df = pd.read_csv(StringIO(csv_data))
print("=== CSV Loaded ===")
print(df)
print(f"nShape: {df.shape}")
print(f"Columns: {list(df.columns)}")
Output
=== CSV Loaded ===
name score city
0 Alice 95 Bangalore
1 Bob 82 Mumbai
2 Charlie 91 Chennai
3 Diana 78 Hyderabad
4 Ethan 88 Pune
Shape: (5, 3)
Columns: ['name', 'score', 'city']
Data Analysis with Pandas
Once loaded, you can perform analysis instantly. No loops needed.
print(f"nAverage score: {df['score'].mean():.2f}")
print(f"Top scorer: {df.loc[df['score'].idxmax(), 'name']}")
print(f"nAbove 85:")
print(df[df['score'] > 85])
Output
Average score: 86.80
Top scorer: Alice
Above 85:
name score city
0 Alice 95 Bangalore
2 Charlie 91 Chennai
4 Ethan 88 Pune
Handling Different Delimiters
Not all CSV files use commas. Tab-separated (TSV) and semicolon-separated files are common — especially data exported from European systems. Use the sep parameter:
# Tab-separated (TSV)
df = pd.read_csv('data.tsv', sep='t')
# Semicolon-separated
df = pd.read_csv('data.csv', sep=';')
# Auto-detect delimiter (Python engine)
df = pd.read_csv('data.csv', sep=None, engine='python')
Common mistake: Writing sep='t' instead of sep='t'. The first uses the letter t as a delimiter — not a tab. Always use a backslash before t for tab characters.
Common read_csv Options
header=0— Row number to use as column names (default: first row)index_col=False— Don’t use any column as the indexusecols=['a', 'b']— Load only specific columnsna_values=['N/A']— Treat ‘N/A’ as missing dataparse_dates=True— Parse columns as datetimeencoding='utf-8'— File encoding
Reading from a Real File
# Read from disk
df = pd.read_csv('students.csv')
# Skip first 2 rows
df = pd.read_csv('students.csv', skiprows=2)
# Load only first 100 rows
df = pd.read_csv('students.csv', nrows=100)
# Handle missing values
df = pd.read_csv('students.csv', na_values=['NA', 'missing', ''])
Common Mistakes / Gotchas
- Mixed type inference: If a column has numbers and strings, pandas may infer it as
objectdtype. Usedtypeparameter to force types explicitly:pd.read_csv('data.csv', dtype={'score': float, 'age': int}) - Unnamed last column: If your CSV has a trailing comma, pandas reads an extra empty column. Check
df.columnsafter loading. - Encoding issues: Files from Windows systems often use
encoding='latin-1'orencoding='cp1252'. UTF-8 is not guaranteed. - Memory spikes: For multi-GB files, use
chunksizeto iterate in batches:for chunk in pd.read_csv('big.csv', chunksize=10000): process(chunk)
When Pandas Is Not Enough — Scaling Up
Pandas loads the entire file into memory. For datasets beyond a few GB, consider switching to Polars, which uses lazy evaluation and can process data larger than RAM. For Python’s native approach, see the csv module documentation.
Summary & Next Steps
You now know how to read CSV files with pandas read_csv(), perform instant analysis on DataFrames, handle different delimiters, and control which data to load. To go further:
- When Pandas Finally Broke Me: How I Discovered Python Polars — scaling beyond pandas for large files
- File handling in Python — reading and writing files with pure Python
- pandas.read_csv() official docs — full parameter reference

