Reading CSV and Excel Files with pandas
Load real-world files correctly — handle encodings, date columns, and multi-sheet Excel
Reading files is the first step of every real data project. In practice, files are messy — wrong encodings, extra header rows, inconsistent null representations, date strings, mixed types. This tutorial covers the parameters you actually need on the job.
Examples
Key Points
- ✓UnicodeDecodeError → try encoding="latin-1" (common with Hindi/Indian data)
- ✓dtype={"PinCode": str} keeps leading zeros in postal codes — otherwise read as int
- ✓parse_dates=["DateCol"] converts string dates to datetime — required for time analysis
- ✓na_values extends the default null list (NaN, None, empty) with custom nulls like "--"
- ✓low_memory=False avoids mixed-type column warnings on large files
Practice Question
A PIN code column (e.g. "011001") is being loaded as the integer 11001, losing the leading zero. Which read_csv parameter fixes this?