将私人数据导入谷歌协作笔记本的常用方法是什么?是否可以导入一个非公开的谷歌表?不能从系统文件中读取。介绍性文档链接到使用BigQuery的指南,但这似乎有点…多。
当前回答
上传数据/导入数据到谷歌colab GUI方式的最佳和简单的方法是点击左边的第3个选项文件菜单图标,在那里你会得到上传浏览器文件,因为你在windows操作系统。检查下面的图像更好地容易理解。点击下面两个选项后,你会很容易地得到上传窗口框。工作。
from google.colab import files
files=files.upload()
其他回答
一个演示本地文件上传/下载以及与Drive和sheets集成的官方示例笔记本可在这里获得: https://colab.research.google.com/notebooks/io.ipynb
共享文件最简单的方法是挂载您的谷歌驱动器。
要做到这一点,在代码单元格中运行以下命令:
from google.colab import drive
drive.mount('/content/drive')
它会要求您访问一个链接,以允许“谷歌文件流”访问您的驱动器。之后,一个长长的字母数字认证代码将显示,需要输入在你的Colab的笔记本。
之后,您的驱动器文件将被挂载,您可以在侧面板中的文件浏览器浏览它们。
这是一个完整的笔记本示例
如果数据集大小小于25mb,最简单的方法是从你的GitHub存储库上传CSV文件。
单击存储库中的数据集 点击“查看原始”按钮 复制链接并将其存储在一个变量中 将变量加载到Pandas read_csv中以获得数据帧
例子:
import pandas as pd
url = 'copied_raw_data_link'
df1 = pd.read_csv(url)
df1.head()
已解决,请在这里找到详细信息,并使用下面的功能: https://stackoverflow.com/questions/47212852/how-to-import-and-read-a-shelve-or-numpy-file-in-google-colaboratory/49467113#49467113
from google.colab import files
import zipfile, io, os
def read_dir_file(case_f):
# author: yasser mustafa, 21 March 2018
# case_f = 0 for uploading one File and case_f = 1 for uploading one Zipped Directory
uploaded = files.upload() # to upload a Full Directory, please Zip it first (use WinZip)
for fn in uploaded.keys():
name = fn #.encode('utf-8')
#print('\nfile after encode', name)
#name = io.BytesIO(uploaded[name])
if case_f == 0: # case of uploading 'One File only'
print('\n file name: ', name)
return name
else: # case of uploading a directory and its subdirectories and files
zfile = zipfile.ZipFile(name, 'r') # unzip the directory
zfile.extractall()
for d in zfile.namelist(): # d = directory
print('\n main directory name: ', d)
return d
print('Done!')
在任何协作的左侧栏上都有一个称为“文件”的部分。 在那里上传文件并使用此路径
"/content/YourFileName.extension"
ex: pd read_csv(“/内容/ Forbes2015。csv”);
下面是一种从谷歌驱动器导入文件到笔记本电脑的方法。
打开jupyter notebook并运行下面的代码并完成身份验证过程
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret= {creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
一旦你完成了上面的代码,运行下面的代码挂载谷歌驱动器
!mkdir -p drive
!google-drive-ocamlfuse drive
从谷歌驱动器导入文件到笔记本(例如:Colab_Notebooks/db.csv)
假设你的数据集文件在Colab_Notebooks文件夹中,它的名字是db.csv
import pandas as pd
dataset=pd.read_csv("drive/Colab_Notebooks/db.csv")
我希望这对你们有帮助