【问题标题】:How to implement this Jupyter notebook on Google Colab?如何在 Google Colab 上实现这个 Jupyter notebook?
【发布时间】:2020-06-06 11:26:18
【问题描述】:

几小时前我才开始使用 Google Colab,我正在尝试了解如何读取、写入和保存内容等。

我在 Jupyter 笔记本上有这段代码,我在保存文件的最后部分遇到了问题,我想将其保存在本地计算机或 Google 云端硬盘上吗?

import pandas as pd

pd.set_option('display.max_columns', 999)

#load data

df = pd.read_csv('D:\\Project\\database\\Isolation Forest\\IF 15 PERCENT.csv')
df.shape

#data info
info = df.info()
print(info)


#data description
describe = df.describe()  #print(describe)
f = open('D:\\Project\\database\\Isolation Forest\\Final Description IF TEST11.txt', "w+")
print(describe, file=f)
f.close()

Google Colab 代码:

import pandas as pd
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
# Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

link = '......'

fluff, id = link.split('=')
print (id) # Verify that you have everything after '='
downloaded = drive.CreateFile({'id':id}) 
downloaded.GetContentFile('IF 15 PERCENT.csv')  
df = pd.read_csv('IF 15 PERCENT.csv',index_col=None)

info = df.info()
print(info)
describe = df.describe()

我现在真的不知道怎么把它保存为txt文件和w+

谢谢。

【问题讨论】:

  • 你是从某个链接下载的吗?
  • @Pygirl 链接指向我的 csv 文件在我的谷歌驱动器中
  • 你遇到了什么问题?

标签: python pandas dataframe machine-learning google-colaboratory


【解决方案1】:

这会将您的数据框保存为文本格式:

tfile = open('test.txt', 'w+')
tfile.write(describe.to_string())
tfile.close()

【讨论】:

  • 如果您已经挂载了 gdrive,那么在您的本地主机上运行的任何东西也将在 colab notebook 中运行。
  • 所以我已经尝试过了,但文本文件是一个长句子,而不是柱形。
猜你喜欢
  • 2018-09-01
  • 2019-11-22
  • 2020-03-20
  • 1970-01-01
  • 2019-08-17
  • 2020-07-21
  • 2018-11-04
  • 1970-01-01
  • 2021-12-24
相关资源
最近更新 更多