③ 讀取HTML表格¶
在這個教學中,我們示範如何從維基百科List of S&P 500 companies
的資料中,將標普五百的公司列表讀出來。
載入模組¶
In [1]:
Copied!
import pandas as pd
import pandas as pd
讀取網址中的HTML表格¶
In [2]:
Copied!
url = "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
url = "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
In [3]:
Copied!
dfs = pd.read_html(url)
dfs = pd.read_html(url)
秀出第一個表格¶
In [4]:
Copied!
dfs[0]
dfs[0]
Out[4]:
Symbol | Security | GICS Sector | GICS Sub-Industry | Headquarters Location | Date added | CIK | Founded | |
---|---|---|---|---|---|---|---|---|
0 | MMM | 3M | Industrials | Industrial Conglomerates | Saint Paul, Minnesota | 1957-03-04 | 66740 | 1902 |
1 | AOS | A. O. Smith | Industrials | Building Products | Milwaukee, Wisconsin | 2017-07-26 | 91142 | 1916 |
2 | ABT | Abbott Laboratories | Health Care | Health Care Equipment | North Chicago, Illinois | 1957-03-04 | 1800 | 1888 |
3 | ABBV | AbbVie | Health Care | Biotechnology | North Chicago, Illinois | 2012-12-31 | 1551152 | 2013 (1888) |
4 | ACN | Accenture | Information Technology | IT Consulting & Other Services | Dublin, Ireland | 2011-07-06 | 1467373 | 1989 |
... | ... | ... | ... | ... | ... | ... | ... | ... |
498 | XYL | Xylem Inc. | Industrials | Industrial Machinery & Supplies & Components | White Plains, New York | 2011-11-01 | 1524472 | 2011 |
499 | YUM | Yum! Brands | Consumer Discretionary | Restaurants | Louisville, Kentucky | 1997-10-06 | 1041061 | 1997 |
500 | ZBRA | Zebra Technologies | Information Technology | Electronic Equipment & Instruments | Lincolnshire, Illinois | 2019-12-23 | 877212 | 1969 |
501 | ZBH | Zimmer Biomet | Health Care | Health Care Equipment | Warsaw, Indiana | 2001-08-07 | 1136869 | 1927 |
502 | ZTS | Zoetis | Health Care | Pharmaceuticals | Parsippany, New Jersey | 2013-06-21 | 1555280 | 1952 |
503 rows × 8 columns