【问题标题】:How to import data into google colab from google drive?如何从谷歌驱动器将数据导入谷歌colab?
【发布时间】:2018-07-19 17:46:27
【问题描述】:

我在我的谷歌驱动器上上传了一些数据文件。 我想将这些文件导入 google colab。

REST API 方法和 PyDrive 方法展示了如何创建新文件并将其上传到 drive 和 colab。使用它,我无法弄清楚如何在我的 python 代码中读取驱动器上已经存在的数据文件。

我完全是新手。有人可以帮帮我吗?

【问题讨论】:

    标签: python jupyter-notebook google-colaboratory


    【解决方案1】:

    (2018 年 4 月 15 日更新:gspread 更新频繁,为确保工作流程稳定,我指定了版本)

    对于电子表格文件,基本思路是使用 gspread 和 pandas 包读取 Drive 中的电子表格并将其转换为 pandas 数据帧格式。

    在 Colab 笔记本中:

    #install packages
    !pip install gspread==2.1.1
    !pip install gspread-dataframe==2.1.0
    !pip install pandas==0.22.0
    
    
    #import packages and authorize connection to Google account:
    import pandas as pd
    import gspread
    from gspread_dataframe import get_as_dataframe, set_with_dataframe
    from google.colab import auth
    auth.authenticate_user()  # verify your account to read files which you have access to. Make sure you have permission to read the file!
    from oauth2client.client import GoogleCredentials
    gc = gspread.authorize(GoogleCredentials.get_application_default()) 
    

    然后我知道了 3 种阅读 Google 电子表格的方法。

    按文件名:

    spreadsheet = gc.open("goal.csv") # Open file using its name. Use this if the file is already anywhere in your drive
    sheet =  spreadsheet.get_worksheet(0)  # 0 means the first sheet in the file
    df2 = pd.DataFrame(sheet.get_all_records())
    df2.head()
    

    通过网址:

     spreadsheet = gc.open_by_url('https://docs.google.com/spreadsheets/d/1LCCzsUTqBEq5pemRNA9EGy62aaeIgye4XxwReYg1Pe4/edit#gid=509368585') # use this when you have the complete url (the edit#gid means permission)
        sheet =  spreadsheet.get_worksheet(0)  # 0 means the first sheet in the file
        df2 = pd.DataFrame(sheet.get_all_records())
        df2.head()
    

    按文件密钥/ID:

    spreadsheet = gc.open_by_key('1vpukIbGZfK1IhCLFalBI3JT3aobySanJysv0k5A4oMg') # use this when you have the key (the string in the url following spreadsheet/d/)
    sheet =  spreadsheet.get_worksheet(0)  # 0 means the first sheet in the file
    df2 = pd.DataFrame(sheet.get_all_records())
    df2.head()
    

    我在 Colab 笔记本中分享了上面的代码: https://drive.google.com/file/d/1cvur-jpIpoEN3vAO8Fd_yVAT5Qgbr4GV/view?usp=sharing

    来源:https://github.com/burnash/gspread

    【讨论】:

    • 安装过程中出现错误:ERROR: gspread-dataframe 3.0.3 has requirement gspread>=3.0.0, but you'll have gspread 2.1.1 which is incompatible.
    【解决方案2】:

    !)然后将您的数据设置为公开可用 对于公共电子表格:

    from StringIO import StringIO  # got moved to io in python3.
    
    import requests
    r = requests.get('https://docs.google.com/spreadsheet/ccc? 
    key=0Ak1ecr7i0wotdGJmTURJRnZLYlV3M2daNTRubTdwTXc&output=csv')
    data = r.content
    
    In [10]: df = pd.read_csv(StringIO(data), index_col=0,parse_dates= 
    ['Quradate'])
    
    In [11]: df.head()
    

    更多:Getting Google Spreadsheet CSV into A Pandas Dataframe

    如果私有数据排序相同,但您将不得不做一些 auth 体操...

    【讨论】:

    • 数据不在谷歌电子表格中。它是一个已保存到磁盘的 numpy 数组。如何处理?
    • 我会采用相同的方法,将文件保存到驱动器并从公共 url 读取它。但是考虑到一些奇怪的可能失真,我会将 NP 数组制作成 pandas 数据框并将其作为 csv 保存到电子表格中,然后按照上述操作,我甚至没有尝试在本地保存和读取 np 数组......
    【解决方案3】:

    来自 Google Colab sn-ps

    from google.colab import auth
    auth.authenticate_user()
    
    import gspread
    from oauth2client.client import GoogleCredentials
    
    gc = gspread.authorize(GoogleCredentials.get_application_default())
    
    worksheet = gc.open('Your spreadsheet name').sheet1
    
    # get_all_values gives a list of rows.
    rows = worksheet.get_all_values()
    print(rows)
    
    # Convert to a DataFrame and render.
    import pandas as pd
    pd.DataFrame.from_records(rows)
    

    【讨论】:

      猜你喜欢
      • 2021-03-25
      • 2020-09-18
      • 2018-10-14
      • 2021-07-07
      • 2021-06-13
      • 1970-01-01
      • 2020-04-30
      • 2019-03-30
      • 2019-10-21
      相关资源
      最近更新 更多