Spreadsheets
Last updated
Much of the world's data lives in spreadsheets made for people to read: titles, merged cells, totals and footnotes. In this lesson you'll read a messy sheet reliably, by skipping what isn't data, filling merged cells and removing the total row, and check the result against the sheet's own total.
By the end of this lesson you can
- Read a sheet with
skiprows, and name its header row - Fill merged cells with
ffill, and drop total and note rows - Check a sheet against its own totals
- Read and write Excel files with
read_excelandto_excel
What a sheet hides
The head office sends a monthly branch report as a spreadsheet. Step through it as pandas sees it:
read_csv or read_excel will misread.Sheets are made to be read by people, so they mix the table with titles, totals and notes. A person skips those without thinking. pandas reads every row as data unless you say otherwise.
Reading it properly
The sheet, saved as CSV, is in data/raw/. Run the cells.
- Read as it is, the title becomes the column names, and the real header is row 2 of the data.
skiprows=3skips the title, the month and the blank row, so the fourth line is the header. The table still ends with the total, a blank row and the footnote.- Remove what isn't data: the row whose province is
Total, then every row with no store (the blank and the footnote). ffill(), forward fill, fills each blank province from the row above. A merged cell in Excel keeps its value only in its first row.
The last cell adds up each column: 4,110 kg of rice, the same as the sheet's own total row. Checking your sum against the sheet's total is the quickest test that you read it right.
Other options worth knowing: usecols reads only some columns, nrows stops after a number of rows, and header=None reads a sheet that has no header at all.
Wrong way: the total counted twice
Suppose you skip the title rows but forget the total. Run it.
8,220 kg: exactly twice the real figure, because the total row is added to the four stores it already sums. No error, and the number looks plausible for a month of rice. A total in the middle of a sheet (one per region, say) is harder still to spot.
The second cell is the fix and the check together: keep the rows that name a store, then compare their sum with the sheet's own total: True. If the head office ever adds a subtotal row, this check is what will tell you.
Excel files on your computer
This page reads CSV, but on your computer you can read the .xlsx file directly. pandas uses a library called openpyxl for it, so install that first in your project's environment:
python -m pip install openpyxl
Then the same options work on a sheet:
report = pd.read_excel("data/raw/branch_report.xlsx", sheet_name="January", skiprows=3)
sheets = pd.read_excel("data/raw/branch_report.xlsx", sheet_name=None)
list(sheets) # every sheet's name: a dict of DataFramessheet_name=None reads every sheet into a dictionary, one DataFrame per sheet: handy when each month is its own sheet, and you can pd.concat them.
Writing works too, for people who want a spreadsheet back:
with pd.ExcelWriter("reports/summary.xlsx") as writer:
by_store.to_excel(writer, sheet_name="By store")
by_week.to_excel(writer, sheet_name="By week")Keep Excel for sharing. For data you'll read again, CSV or Parquet is simpler and safer (lesson 3.3): Excel quietly turns some text into dates, and long codes into rounded numbers.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- pandas: pandas.read_excel: sheet_name, header, skiprows and usecols
- pandas: pandas.read_csv: the same options for CSV files
- openpyxl documentation: the library pandas uses for .xlsx files
- Broman and Woo, Data organization in spreadsheets (The American Statistician, 2018): how to make sheets that read cleanly