Reading HTML tables in Data Analysis Python - Time & Space Complexity
When we read HTML tables into data, we want to know how long it takes as the table size grows.
We ask: How does the time to read change when the table has more rows or columns?
Analyze the time complexity of the following code snippet.
import pandas as pd
url = 'https://example.com/sample-table.html'
tables = pd.read_html(url)
df = tables[0]
This code reads all HTML tables from a webpage and selects the first table as a DataFrame.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Parsing each row and column of the HTML table to build the DataFrame.
- How many times: Once for each cell in the table (rows x columns).
As the number of rows and columns grows, the work grows with the total number of cells.
| Input Size (rows x columns) | Approx. Operations |
|---|---|
| 10 x 5 = 50 | About 50 operations |
| 100 x 10 = 1,000 | About 1,000 operations |
| 1,000 x 20 = 20,000 | About 20,000 operations |
Pattern observation: The time grows roughly in direct proportion to the number of cells in the table.
Time Complexity: O(n * m)
This means the time to read the table grows proportionally to the number of rows (n) times the number of columns (m).
[X] Wrong: "Reading an HTML table takes the same time no matter how big it is."
[OK] Correct: The code must process each cell, so bigger tables take more time to read.
Understanding how data reading time grows helps you explain your code's efficiency clearly and confidently.
"What if the HTML table has nested tables inside cells? How would the time complexity change?"