为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

小怪兽爱吃肉 2022-09-13 15:23:50
我一直试图从这个公共谷歌表链接中获取工作表作为python数据帧。Statewise此工作表的 URL 不同于其他示例的 URL,用于实现将工作表作为数据帧(在此网站上看到)的目标。网址是这个: https://docs.google.com/spreadsheets/d/e/2PACX-1vSc_2y5N0I67wDU38DjDh35IZSIS30rQf7_NYZhtYYGU1jJYT6_kDx4YpF-qw0LSlGsBYP8pqM_a1Pd/pubhtml#一种标准方式可能是以下几点,import pandasgoogleSheetId = '<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中,我没有看到这里使用的结构。有人可以帮忙澄清吗?谢谢。
查看完整描述

2 回答

?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

谷歌电子表格实际上是一个html的东西。因此,您应该使用将其加载到熊猫数据帧列表中:read_html


dfs = pd.read_html(url, encoding='utf8')

如果 lxml 可用,或者如果您使用美丽汤 4:


dfs = pd.read_html(url, flavor='bs4', encoding='utf8')

您将获得数据帧列表,例如:dfs[0]


     0   1                                                  2                3

0    1  id                                             Banner  Number_Of_Times

1    2   1  Don't Hoard groceries and essentials. Please e...                2

2    3   2  Be compassionate! Help those in need like the ...                2

3    4   3  Be considerate : While buying essentials remem...                2

4    5   4  Going out to buy essentials? Social Distancing...                2

5    6   5  Plan ahead! Take a minute and check how much y...                2

6    7   6  Plan and calculate your essential needs for th...                2

7    8   7  Help out the elderly by bringing them their gr...                2

8    9   8  Help out your workers and domestic help by not...                2

9   10   9  Lockdown means LOCKDOWN! Avoid going out unles...                1

10  11  10           Panic mode : OFF! ❌ESSENTIALS ARE ON! ✔️                1

11  12  11  Do not panic! ❌ Your essential needs will be t...                1

12  13  12  Be a true Indian. Show compassion. Be consider...                1

13  14  13  If you have symptoms and suspect you have coro...                1

14  15  14  Stand Against FAKE News and WhatsApp Forwards!...                1

15  16  15  If you have any queries, Reach out to your dis...                1


查看完整回答
反对 回复 2022-09-13
?
泛舟湖上清波郎朗

TA贡献1818条经验 获得超3个赞

您可以使用以下代码段:


from io import BytesIO

import requests


r = requests.get(URL)

data = r.content


df = pd.read_csv(BytesIO(data), index_col=0, error_bad_lines=False)


查看完整回答
反对 回复 2022-09-13
  • 2 回答
  • 0 关注
  • 68 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信