read_html
read_html finds every table on a web page (or HTML string) and returns them as DataFrames.
In this page:
Syntax
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
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
- Forgetting it returns a list
- Missing lxml or html5lib
- 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: