【问题标题】:After converting a data frame to csv file in colab,I cannot save the file to my google drive在 colab 中将数据框转换为 csv 文件后,我无法将文件保存到我的谷歌驱动器
【发布时间】:2022-01-07 08:40:00
【问题描述】:
import pandas as pd

df = pd.read_excel('/content/drive/MyDrive/Colab Notebooks/Files /Copy of weather_data.xlsx')
k2 = df.head(4)
k2.to_csv('/content/drive/MyDrive/Colab Notebooks/Files/new_fileee.csv',index=False)

这会产生一个错误,指出没有这样的文件或目录

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: pandas google-colaboratory


【解决方案1】:

通常,每个人都将错误代码放在消息中,而不是图片中。您是否查看过这些网站 - Pandas Dataframe 到 CSV 文件 - 使用 .to_csv() 导出 https://datagy.io/pandas-dataframe-to-csv/; pandas.read_excel -https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html

看看你的错误,我假设你已经测试了你的输出......

print(k2)

也许尝试使用 os.path.dirname 获取路径

import pandas as pd
import os

df = pd.read_excel("/content/drive/MyDrive/Colab Notebooks/Files/Copy of the weather.xlsx")
k2 = df.head()
dirname = os.path.dirname("content/drive/MyDrive/Colab Notebooks/Files/Copy of the weather.xlsx")
newfilename = "new_fileee.csv"
csvfile = dirname + "/" + newfilename
k2.to_csv(csvfile, index = false)

【讨论】:

  • 感谢您抽出宝贵的时间。不幸的是,这不起作用,同样的错误返回说没有这样的文件或目录。
  • 测试您当前的文件路径 -- split = os.path.split("/content/drive/MyDrive/Colab Notebooks/Files/Copy of the weather.xlsx") print(split). print(split[0]). print(split[1])。你得到输出了吗?
  • ('/content/drive/MyDrive/Colab Notebooks/Files', '天气的副本.xlsx') /content/drive/MyDrive/Colab Notebooks/Files 天气的副本.xlsx 这是输出
  • 如果将分割线添加到代码中,将dirname = os.path.dirname("/content/drive/MyDrive/Colab Notebooks/Files/Copy of the weather.xlsx") 更新为dirname = split[0] 会发生什么?
猜你喜欢
  • 1970-01-01
  • 2019-10-21
  • 1970-01-01
  • 1970-01-01
  • 2021-06-13
  • 2020-08-31
  • 1970-01-01
  • 2020-07-30
  • 1970-01-01
相关资源
最近更新 更多