Reading CSV Files
Until now sales was typed by hand. In real life the bookshop's till exports a file called sales.csv every night, and your analysis starts by loading it. CSV is the most common data file in the world, and also the one most likely to be slightly wrong.
The rule
read_csvguesses; tell it what it cannot know, then check what it built.
A CSV file is plain text: one line per row, values separated by commas, usually with a header line of column names.
date,branch,genre,copies,price
2024-03-01,Pune,fiction,3,250
2024-03-01,Delhi,science,5,400sales = pd.read_csv("sales.csv")pandas assumes values are split by commas and that the first line is the header, and it guesses each column's type from the values. When the file is unusual, you supply the facts it cannot guess:
pd.read_csv(
"sales_export.csv",
sep=";", # values split by semicolons, common in Europe
skiprows=2, # two lines of title text before the header
usecols=["date", "branch", "copies"], # load only these columns
parse_dates=["date"], # turn the date text into real dates
na_values=["-", "unknown"], # treat these markers as missing
encoding="utf-8",
)Then check what it built, every time:
sales.shape # did every row arrive?
sales.head() # do the columns look right?
sales.dtypes # are numbers numbers?
sales.info() # types plus non-missing counts per columnWriting goes the other way: sales.to_csv("clean_sales.csv", index=False). Without index=False, the 0, 1, 2… index is saved as an extra unnamed column.
The shortcut
After every load, run the three-glance check: shape, head(), dtypes. Most CSV problems show up in one of them within ten seconds.
Load it
pd.read_csv("sales_export.csv")runs with no error.
Another classic: a copies column showing dtype object instead of int64. Some cell holds text such as "unknown". Adding it to na_values turns it into a proper missing value, and the column becomes numeric (as floats, because NaN is a float).
Drills
Answer fast — trust the rule.
A CSV loads with shape (500, 1) and every cell contains semicolons. What is the fix?
Why pass index=False to to_csv?
price loads as dtype object. What is the most likely cause?
A column has values 3, 5 and "-". After loading with na_values=["-"], what is its dtype?
Remember
read_csvguesses; tell it what it cannot know, then check what it built.- Three-glance check after every load:
shape,head(),dtypes. - Write with
index=Falseunless the index carries real information.
