Alex Borland Alex Borland -4 years ago 111
Python Question

How can I select the second dataframe only from a webpage with two dataframes, using pandas in python?

I am trying to only use the second dataframe on this url, but I cannot figure out how to specify to only get the one dataframe. It prints out the dataframe of the chart and the table, but all I want it to print out is the table.

`import pandas as pd
import urllib.request

page = pd.read_html('https://www.google.com/finance/historical?q=a&startdate=Jan%201%2C%202000&enddate=Feb%2028%2C%202017&num=200&ei=_nm3WKGHCIf7jAG74ar4Cw&start=200', header=0)

for df in page:
print(df)`

Answer Source

The table you are looking for has a class attribute gf-table, pass it to pd.read_html, then it will only read the second table:

page = pd.read_html('https://www.google.com/finance/historical?q=a&startdate=Jan%201%2C%202000&enddate=Feb%2028%2C%202017&num=200&ei=_nm3WKGHCIf7jAG74ar4Cw&start=200', 
                    attrs = {'class': 'gf-table'},
                    header=0)
‚Äč
page

#             Date   Open   High    Low  Close   Volume
#0    May 12, 2016  42.59  42.94  42.42  42.73  2224506
#1    May 11, 2016  42.19  43.20  42.12  42.46  3325515
#2    May 10, 2016  41.50  42.00  41.35  42.00  2094305
#3     May 9, 2016  41.51  41.78  41.29  41.33  1741539
#4     May 6, 2016  40.86  41.62  40.72  41.43  1403476
#5     May 5, 2016  40.64  41.03  40.51  40.96  1083956
#...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download