将私人数据导入谷歌协作笔记本的常用方法是什么?是否可以导入一个非公开的谷歌表?不能从系统文件中读取。介绍性文档链接到使用BigQuery的指南,但这似乎有点…多。


当前回答

上传数据/导入数据到谷歌colab GUI方式的最佳和简单的方法是点击左边的第3个选项文件菜单图标,在那里你会得到上传浏览器文件,因为你在windows操作系统。检查下面的图像更好地容易理解。点击下面两个选项后,你会很容易地得到上传窗口框。工作。

from google.colab import files
files=files.upload()

其他回答

您可以通过运行以下命令挂载到谷歌驱动器 从谷歌。Colab导入驱动器 drive.mount(/内容/驱动器) 训练后复制数据从gdrive到colab根文件夹。

cp -r '/content/drive/My drive/ Project_data' '/content'

其中第一个路径是gdrive路径,第二个是colab根文件夹。

这种方法对于大数据的训练速度更快。

一个演示本地文件上传/下载以及与Drive和sheets集成的官方示例笔记本可在这里获得: https://colab.research.google.com/notebooks/io.ipynb

共享文件最简单的方法是挂载您的谷歌驱动器。

要做到这一点,在代码单元格中运行以下命令:

from google.colab import drive
drive.mount('/content/drive')

它会要求您访问一个链接,以允许“谷歌文件流”访问您的驱动器。之后,一个长长的字母数字认证代码将显示,需要输入在你的Colab的笔记本。

之后,您的驱动器文件将被挂载,您可以在侧面板中的文件浏览器浏览它们。

这是一个完整的笔记本示例

您可以使用下面的函数。我假设您正在尝试上传一个数据帧类型的文件(.csv, .xlsx)

def file_upload():
    file = files.upload()
    path = f"/content/{list(file.keys())[0]}"
    df = pd.read_excel(path)
    return df

#your file will be saved in the variable: dataset
dataset = file_upload()

这是在你没有改变谷歌合作目录的情况下,这是最简单的方法

步骤1-挂载您的谷歌驱动器到协作实验室

from google.colab import drive
drive.mount('/content/gdrive')

第2步-现在你会看到你的谷歌驱动器文件在左侧窗格(文件资源管理器)。右键单击需要导入的文件并选择çopy路径。 然后像往常一样在pandas中导入,使用这个复制的路径。

import pandas as pd
df=pd.read_csv('gdrive/My Drive/data.csv')

完成了!

我创建了一小段代码,可以以多种方式实现这一点。你可以

使用已经上传的文件(在重新启动内核时很有用) 使用来自Github的文件 手动上传文件

import os.path

filename = "your_file_name.csv"
if os.path.isfile(filename):
  print("File already exists. Will reuse the same ...")
else:
  use_github_data = False  # Set this to True if you want to download from Github
  if use_github_data:
    print("Loading fie from Github ...")
    # Change the link below to the file on the repo
    filename = "https://github.com/ngupta23/repo_name/blob/master/your_file_name.csv" 
  else:
    print("Please upload your file to Colab ...")
    from google.colab import files
    uploaded = files.upload()