Tables to pandas

Turn parsed rows into a DataFrame.

Once you have a header and rows, pd.DataFrame(rows, columns=header) is a normal table. From there you can astype numbers and filter — that is the pandas course.

Goal

Build a DataFrame from an HTML table and print dtypes after converting units.

DataFrame

html = """
<table>
  <tr><th>city</th><th>product</th><th>units</th><th>price</th></tr>
  <tr><td>Nairobi</td><td>A</td><td>12</td><td>10.5</td></tr>
  <tr><td>Mombasa</td><td>A</td><td>9</td><td>10.5</td></tr>
  <tr><td>Kisumu</td><td>B</td><td>11</td><td>22.0</td></tr>
</table>
"""
soup = BeautifulSoup(html, "html.parser")
header = [th.get_text(strip=True) for th in soup.select("tr")[0].find_all("th")]
rows = [
    [td.get_text(strip=True) for td in tr.find_all("td")]
    for tr in soup.select("tr")[1:]
]
df = pd.DataFrame(rows, columns=header)
df["units"] = pd.to_numeric(df["units"])
df["price"] = pd.to_numeric(df["price"])
df["revenue"] = df["units"] * df["price"]
print(df)
print()
print(df.dtypes)

Do not use pd.read_html here — it wants extra parsers this editor does not load. Soup → lists → DataFrame is the reliable path.

You should see

Attach kiosk.html and parse #sales the same way in Practice.