在Colaboratory中从驱动器加载xlsx文件

13

如何从Google Drive导入MS-Excel(.xlsx)文件到Colaboratory?

excel_file = drive.CreateFile({'id':'some id'})

这段代码中的drive是一个pydrive.drive.GoogleDrive对象,does_work()函数可以正常工作。

print excel_file.FetchContent()

返回 None。并且

excel_file.content()

异常:

TypeErrorTraceback (most recent call last) in () ----> 1 excel_file.content()

TypeError: '_io.BytesIO' 对象不可调用

我的意图是(给定某个有效的文件 'id')将其导入为一个 io 对象,可以被 pandas 的 read_excel() 读取,并最终得到一个 pandas 数据帧。

6个回答

10
你需要使用excel_file.GetContentFile将文件保存在本地,然后你可以在安装了xlrd之后使用Pandas的read_excel方法来读取Excel文件。以下是完整示例:https://colab.research.google.com/notebook#fileId=1SU176zTQvhflodEzuiacNrzxFQ6fWeWC。 更详细的步骤如下: 我在Sheets中创建了一个新的电子表格,并将其导出为.xlsx文件。 接下来,我将其再次导出为.xlsx文件并上传到Drive。URL是:https://drive.google.com/open?id=1Sv4ib5i7CKWhAHZkKg-uitIkS3xwxtXM 请注意文件ID。在我的情况下,它是1Sv4ib5i7CKWhAHZkKg-uitIkS3xwxtXM。 然后,在Colab中,我调整了Drive下载代码段以下载文件。关键部分如下:
file_id = '1Sv4ib5i7CKWhAHZkKg-uitIkS3xwxtXM'
downloaded = drive.CreateFile({'id': file_id})
downloaded.GetContentFile('exported.xlsx')

最后,要创建一个Pandas DataFrame:

!pip install -q xlrd
import pandas as pd
df = pd.read_excel('exported.xlsx')
df

!pip install...这行代码会安装xlrd库,该库用于读取Excel文件。


既然您已经将文件上传到Google Drive并转换为Sheet,那么您可以跳过再次上传的步骤,并像此解决方案中所示一样从Colab中访问它:https://dev59.com/_FYM5IYBdhLWcg3wjAmw#49397059 - Mikeumus

10
也许有一种更简单的方法:
#To read/write data from Google Drive:
#Reference: https://colab.research.google.com/notebooks/io.ipynb#scrollTo=u22w3BFiOveAå
from google.colab import drive
drive.mount('/content/drive')

df = pd.read_excel('/content/drive/My Drive/folder_name/file_name.xlsx')

# #When done, 
# drive.flush_and_unmount()
# print('All changes made in this colab session should now be visible in Drive.')


4

首先,我从 google.colab 中导入 iopandasfiles

import io
import pandas as pd
from google.colab import files

然后我使用上传小部件上传文件

uploaded = files.upload()

你会看到类似于这样的界面(点击“选择文件”并上传xlsx文件): enter image description here 假设文件名为my_spreadsheet.xlsx,那么你需要在下面的代码中使用它:
df = pd.read_excel(io.BytesIO(uploaded.get('my_spreadsheet.xlsx')))

好的,现在您已经拥有了 df 数据框中的第一张表。但是,如果您有多张表格,可以将代码更改为以下内容:

首先,将 io 调用移至另一个变量中。

xlsx_file = io.BytesIO(uploaded.get('my_spreadsheet.xlsx'))

然后,使用新变量来指定工作表名称,就像这样:

df_first_sheet = pd.read_excel(xlsx_file, 'My First Sheet')
df_second_sheet = pd.read_excel(xlsx_file, 'My Second Sheet')

3
import pandas as pd

xlsx_link = 'https://docs.google.com/spreadsheets/d/1Sv4ib5i7CKWhAHZkKg-uitIkS3xwxtXM/export'
df = pd.read_excel(xlsx_link)

如果xlsx文件托管在Google Drive上,一旦共享,任何人都可以使用链接访问它,无论是否有Google账户。不需要使用google.colab.drivegoogle.colab.files依赖项。

"/export" 可以使用。您能分享一下您在哪里找到这方面的信息吗? - Vishesh Mangla
1
你必须将其变成公共文档,我相信。 - s2t2

0

到目前为止,我发现这是最简单的方法。

与我们在桌面上所做的非常相似。

考虑到您将文件上传到Google Drive文件夹中:

  • 在左侧栏中单击“文件”(在{x}下方)
  • 选择Mount Driver > drive > folder > file(左键单击并复制路径)

之后只需转到代码并粘贴路径即可

pd.read_excel('/content/drive/MyDrive/Colab Notebooks/token_rating.xlsx')

0

我找到的最简单的方法

  1. 首先挂载您的Google Drive

    import pandas as pd
    from google.colab import drive
    drive.mount('/content/gdrive')
    
  2. 获取文件路径并使用pd.read_excel()

    df = pd.read_excel('/content/gdrive/MyDrive/data.xlsx')

    df.head()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接