从URL将Excel工作簿的表单导入`pandas.DataFrame`
在查看了不同的方法来读取指向 .xls 文件的链接后,我决定使用 xlrd 这个库。
我现在遇到一个问题,就是把 'xlrd.book.Book' 这种类型的数据转换成 'pandas.DataFrame'。
我有以下的代码:
import pandas
import xlrd
import urllib2
link ='http://www.econ.yale.edu/~shiller/data/chapt26.xls'
socket = urllib2.urlopen(link)
#this line gets me the excel workbook
xlfile = xlrd.open_workbook(file_contents = socket.read())
#storing the sheets
sheets = xlfile.sheets()
我想要获取 sheets
中的最后一个表格,并将其导入为 pandas.DataFrame
,有没有什么好的方法可以做到这一点?我试过使用 pandas.ExcelFile.parse()
,但它需要一个 Excel 文件的路径。当然,我可以把文件保存到内存中,然后再解析(用 tempfile
或其他方法),但我想遵循 Python 的编程风格,使用 pandas 中可能已经写好的功能。
任何建议都非常感谢!
2 个回答
11
你可以把一个网址传给 pandas.read_excel()
这个函数:
import pandas as pd
link ='http://www.econ.yale.edu/~shiller/data/chapt26.xls'
data = pd.read_excel(link,'sheetname')
28
你可以把你的 socket
传递给 ExcelFile
:
>>> import pandas as pd
>>> import urllib2
>>> link = 'http://www.econ.yale.edu/~shiller/data/chapt26.xls'
>>> socket = urllib2.urlopen(link)
>>> xd = pd.ExcelFile(socket)
NOTE *** Ignoring non-worksheet data named u'PDVPlot' (type 0x02 = Chart)
NOTE *** Ignoring non-worksheet data named u'ConsumptionPlot' (type 0x02 = Chart)
>>> xd.sheet_names
[u'Data', u'Consumption', u'Calculations']
>>> df = xd.parse(xd.sheet_names[-1], header=None)
>>> df
0 1 2 3 4
0 Average Real Interest Rate: NaN NaN NaN 1.028826
1 Geometric Average Stock Return: NaN NaN NaN 0.065533
2 exp(geo. Avg. return) NaN NaN NaN 0.067728
3 Geometric Average Dividend Growth NaN NaN NaN 0.012025