Reading and Saving CSV and Excel
Real data comes in files. pd.read_csv reads a CSV file into a DataFrame. Here's the small sales file used in the rest of this course:
Real data arrives as files - CSV exports, Excel reports, JSON from APIs - and pandas reads all of them into DataFrames in one line. Reading options like parse_dates and usecols save cleaning work later.
Example
order_id,date,city,category,product,qty,price
1001,2026-01-03,Delhi,Electronics,Earbuds,1,1999
1002,2026-01-05,Mumbai,Grocery,Rice 5kg,2,420
1003,2026-01-09,Delhi,Books,Novel,3,350
1004,2026-01-15,Pune,Electronics,Power Bank,1,1299
1005,2026-01-22,Mumbai,Electronics,Earbuds,2,1999
1006,2026-02-02,Pune,Grocery,Tea 1kg,1,560
1007,2026-02-07,Delhi,Grocery,Rice 5kg,1,420
1008,2026-02-11,Mumbai,Books,Notebook Set,4,180
1009,2026-02-18,Pune,Books,Novel,1,350
1010,2026-02-25,Delhi,Electronics,Smart Watch,1,2999
1011,2026-03-04,Mumbai,Grocery,Tea 1kg,3,560
1012,2026-03-10,Delhi,Books,Notebook Set,2,180
1013,2026-03-16,Pune,Electronics,Earbuds,1,1999
1014,2026-03-21,Mumbai,Electronics,Smart Watch,1,2999
1015,2026-03-28,Delhi,Grocery,Tea 1kg,2,560Example
Useful options
sep=";"- a different separatorusecols=["city", "qty"]- read only some columnsnrows=1000- read only the first rows of a huge fileencoding="utf-8"- for Hindi or other non-English textpd.read_csv("https://...")- read straight from a web address
Saving
Example
Excel files
Example
pip install openpyxl
df = pd.read_excel("report.xlsx", sheet_name="Jan")
df.to_excel("summary.xlsx", sheet_name="Summary", index=False)Other formats:
read_json, read_sql (from a database), read_parquet (fast and small for big data).Key points
- pd.read_csv and pd.read_excel load files.
- parse_dates turns columns into dates while reading.
- to_csv(index=False) saves results.
📝 Test yourself
1. Which function reads a CSV file?
2. Why pass index=False to to_csv?
3. Fill in the option that turns a column into dates while reading:
pd.read_csv("sales.csv", =["date"])
