如何将数据从谷歌驱动器导入谷歌 colab? [英] How to import data into google colab from google drive?

查看:18
本文介绍了如何将数据从谷歌驱动器导入谷歌 colab?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some data files uploaded on my google drive. I want to import those files into google colab.

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

The REST API method and PyDrive method show how to create a new file and upload it on drive and colab. Using that, I am unable to figure out how to read the data files already present on my drive in my python code.

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

I am a total newbie to this. Can someone help me out?

推荐答案

(2018 年 4 月 15 日更新:gspread 经常更新,所以为了确保稳定的工作流程我指定了版本)

(Update April 15 2018: The gspread is frequently being updated, so to ensure stable workflow I specify the version)

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

For spreadsheet file, the basic idea is using packages gspread and pandas to read spreadsheets in Drive and convert them to pandas dataframe format.

在 Colab 笔记本中:

In the Colab notebook:

#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 电子表格的方法.

Then I know 3 ways to read Google spreadsheets.

按文件名:

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

I shared the code above in a Colab notebook: https://drive.google.com/file/d/1cvur-jpIpoEN3vAO8Fd_yVAT5Qgbr4GV/view?usp=sharing

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

这篇关于如何将数据从谷歌驱动器导入谷歌 colab?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆