将公共谷歌表格作为熊猫数据框架

我一直试图从这个公共谷歌表链接中获取工作表作为python数据帧。Statewise


此工作表的 URL 不同于其他示例的 URL,用于实现将工作表作为数据帧(在此网站上看到)的目标。


网址是这个: https://docs.google.com/spreadsheets/d/e/2PACX-1vSc_2y5N0I67wDU38DjDh35IZSIS30rQf7_NYZhtYYGU1jJYT6_kDx4YpF-qw0LSlGsBYP8pqM_a1Pd/pubhtml#


一种标准方式可能是以下几点,


import pandas


googleSheetId = '<Google Sheets Id>'

worksheetName = '<Sheet Name>'

URL = 'https://docs.google.com/spreadsheets/d/{0}/gviz/tq?tqx=out:csv&sheet={1}'.format(

    googleSheetId,

    worksheetName

)


df = pandas.read_csv(URL)

print(df)

但在目前的URL中,我没有看到这里使用的结构。有人可以帮忙澄清吗?谢谢。


小怪兽爱吃肉
浏览 69回答 2
2回答

慕尼黑5688855

谷歌电子表格实际上是一个html的东西。因此,您应该使用将其加载到熊猫数据帧列表中:read_htmldfs = pd.read_html(url, encoding='utf8')如果 lxml 可用,或者如果您使用美丽汤 4:dfs = pd.read_html(url, flavor='bs4', encoding='utf8')您将获得数据帧列表,例如:dfs[0]&nbsp; &nbsp; &nbsp;0&nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 30&nbsp; &nbsp; 1&nbsp; id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Banner&nbsp; Number_Of_Times1&nbsp; &nbsp; 2&nbsp; &nbsp;1&nbsp; Don't Hoard groceries and essentials. Please e...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 22&nbsp; &nbsp; 3&nbsp; &nbsp;2&nbsp; Be compassionate! Help those in need like the ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 23&nbsp; &nbsp; 4&nbsp; &nbsp;3&nbsp; Be considerate : While buying essentials remem...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 24&nbsp; &nbsp; 5&nbsp; &nbsp;4&nbsp; Going out to buy essentials? Social Distancing...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 25&nbsp; &nbsp; 6&nbsp; &nbsp;5&nbsp; Plan ahead! Take a minute and check how much y...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 26&nbsp; &nbsp; 7&nbsp; &nbsp;6&nbsp; Plan and calculate your essential needs for th...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 27&nbsp; &nbsp; 8&nbsp; &nbsp;7&nbsp; Help out the elderly by bringing them their gr...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 28&nbsp; &nbsp; 9&nbsp; &nbsp;8&nbsp; Help out your workers and domestic help by not...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 29&nbsp; &nbsp;10&nbsp; &nbsp;9&nbsp; Lockdown means LOCKDOWN! Avoid going out unles...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 110&nbsp; 11&nbsp; 10&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Panic mode : OFF! ❌ESSENTIALS ARE ON! ✔️&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 111&nbsp; 12&nbsp; 11&nbsp; Do not panic! ❌ Your essential needs will be t...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 112&nbsp; 13&nbsp; 12&nbsp; Be a true Indian. Show compassion. Be consider...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 113&nbsp; 14&nbsp; 13&nbsp; If you have symptoms and suspect you have coro...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 114&nbsp; 15&nbsp; 14&nbsp; Stand Against FAKE News and WhatsApp Forwards!...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 115&nbsp; 16&nbsp; 15&nbsp; If you have any queries, Reach out to your dis...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1

泛舟湖上清波郎朗

您可以使用以下代码段:from io import BytesIOimport requestsr = requests.get(URL)data = r.contentdf = pd.read_csv(BytesIO(data), index_col=0, error_bad_lines=False)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python