【问题标题】:Why colab notebook does not recognise a file in a set working directory?为什么 colab notebook 无法识别设置工作目录中的文件?
【发布时间】:2021-08-03 16:25:57
【问题描述】:

我尝试使用以下方法运行 python 脚本:

  • 在本地同时使用 spyder 和命令提示窗口以获取 anaconda 和 cmd
  • 然后我使用了 google colab notebook

基本上,我将所有 .nc 文件上传到我的谷歌驱动器以及一个包含代码中需要的一些数据的 excel 文件。代码在某个点停止并返回一条错误消息,指出没有这样的文件,尽管文件显然在那里。

代码如下:

import os
from netCDF4 import Dataset
import numpy as np
import pandas as pd

os.chdir('/content/drive/MyDrive/Precipitations') # to set a new working directory

date_range=pd.date_range(start="19960101", end="20200930")
df = pd.read_excel('/content/drive/MyDrive/Precipitations/stationssa.xlsx')df1=pd.DataFrame(0,columns=df.loc[:,"NAME"],index=date_range)

for position in stations:
    lat_station = df.iloc[position,1]
    lon_station = df.iloc[position,2]
    for day in date_range:
        data = Dataset("gpcp_v01r03_daily_d"+str(day)[0:4]+str(day)[5:7]+str(day)[8:10]+".nc")
        prcp=data.variables['precip'][:]
        lon_data = data.variables['longitude'][:]
        lat_data = data.variables['latitude'][:]
        sq_diff_lat = (lat_data - lat_station)**2
        sq_diff_lon = (lon_data - lon_station)**2
        min_index_lat = sq_diff_lat.argmin()
        min_index_lon = sq_diff_lon.argmin()
        if type(prcp[0, min_index_lat, min_index_lon])==np.ma.core.MaskedConstant:
            df1.loc[str(day),position] = -1
        else:
            df1.loc[str(day),position] = float(prcp[0, min_index_lat, min_index_lon])
df1=df1.replace(-1,np.nan) ### when the dataframe is done
df1.to_csv("Preciptations.csv") ## to export to a csv file 

colab 笔记本的链接在这里:

但我收到此错误消息:

enter image description here

【问题讨论】:

    标签: python pandas dataframe google-colaboratory netcdf4


    【解决方案1】:

    你需要先安装你的驱动器,使用

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

    【讨论】:

      猜你喜欢
      • 2021-05-13
      • 2017-12-02
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 2020-03-30
      • 2021-06-03
      • 1970-01-01
      相关资源
      最近更新 更多