[Python] Export to netcdf / .nc file from a pandas DataFrame

Discussão em 'Python' iniciado por Stack, Janeiro 31, 2025.

  1. Stack

    Stack Membro Participativo

    I want to multiple data extract from cmip6 model data and save as netcdf file by using the following scripts:

    import pandas as pd
    import xarray as xr

    from netCDF4 import Dataset

    nc_file = (r"C:\Users\DELL 3090\Desktop\Projection\RF_58\pr_Amon_AWI-CM-1-1-MR_ssp585_mm_month.nc")
    NC = xr.open_dataset(nc_file)

    #define locations and station id
    lat=[13.3,7.5,8.5,7.9,9,7.8,11.3,14.3,14.2,7]

    lon=[39.8,34.3,39.8,38.7,38.8,39.9,37.5,39.5,38.9,39.9]

    name=['Abala','Abobo','Abomsa','Adamitulu','AddisAbabaBole','Adele','Adet','Adigrat','Adwa','Agarfa']

    Newdf = pd.DataFrame([])

    for i,j,id in zip(lat,lon,name):
    dsloc = NC.sel(lat=i,lon=j,method='nearest')
    DT=dsloc.to_dataframe()
    # insert the name with your preferred column title:
    Newdf=Newdf._append(DT,sort=True)
    Newdf.to_netcdf('C:/Users/DELL 3090/Desktop/Projection\RF_58/pr_Amon_AWI-CM-1-1 MR_ssp585_mm_month111.nc', index=True, header=True)


    But finally python responds this error:


    File ~\anaconda3\envs\env_resaerch\Lib\site-packages\pandas\core\generic.py:6204 in __getattr__ return object.__getattribute__(self, name)
    AttributeError: 'DataFrame' object has no attribute 'to_netcdf'
    ​

    so please help me what can I do?

    Continue reading...

Compartilhe esta Página