Getting and cleaning data · lesson 2 of 11
Parts in this lesson
Apd.read_excel(path, sheet_name="January", skiprows=3)one sheet, from its header row
Bs.ffill()fill blanks from the row above

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_excel and to_excel

What a sheet hides

The head office sends a monthly branch report as a spreadsheet. Step through it as pandas sees it:

A spreadsheet, as pandas reads it. A sheet as a person reads it: a title, the month, a table, a total and a footnote. Everything a person needs, and several things that read_csv or read_excel will misread.Branch reportJanuary 2024provincestorerice_kgsugar_kgoil_kgBatangasBatangas1420355114Lipa106526685Tanauan92023074Rosario70517656Total41101027329Rosario: estimatesbranch_report.xlsxABCDE1234567891011
1 of 6 A sheet as a person reads it: a title, the month, a table, a total and a footnote. Everything a person needs, and several things that 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.

branch.ipynb
  • Read as it is, the title becomes the column names, and the real header is row 2 of the data.
  • skiprows=3 skips 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.

total.ipynb

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:

Terminal, in the tindahan folder
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 DataFrames

sheet_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

A sheet has a two-line title and a blank line before its header. Which reads it?
After reading a sheet, the province column is blank on most rows. Why, and what fixes it?
What’s the quickest check that a sheet was read correctly?

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.

All sources for Simple Data Analysis