← Back to Pandas Course | Chapter 11: File I/O | Lesson 5 of 7

read_html

read_html finds every table on a web page (or HTML string) and returns them as DataFrames.

In this page:

  1. read_html
Syntax
python
tables = pd.read_html("page.html")
df = tables[0]

read_html

pd.read_html returns a list of DataFrames, one per table it finds. It needs the lxml parser (or BeautifulSoup with html5lib). Pass a URL, file or HTML string wrapped in StringIO. Use match to pick a table containing certain text.

Note: Choose the table you need with the list index, e.g. tables[0].

Example: read_html

python
import io
import lxml
import pandas as pd

html = "<table><tr><th>city</th><th>pop</th></tr><tr><td>Oslo</td><td>700</td></tr><tr><td>Rome</td><td>2800</td></tr></table>"
tables = pd.read_html(io.StringIO(html))
print(len(tables))
print(tables[0])

# Output:
# 1
#    city   pop
# 0  Oslo   700
# 1  Rome  2800
Related Topics
Common Mistakes
  1. Forgetting it returns a list
  2. Missing lxml or html5lib
  3. Passing a raw HTML string directly in newer pandas
Chapter Summary
  • read_html returns a list of DataFrames
  • Needs lxml or bs4 plus html5lib
  • Wrap strings in StringIO
  • match filters tables
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.