使用 xarray 将 csv 文件转换为 nc(netcdf) 文件

飞跃

我想将存储日期、温度值、纬度和经度信息的 CSV 文件转换为具有三维的 NetCDF 文件格式。

我的数据框是这样的:

在此处输入图片说明

当我在下面使用这个脚本时,它只包含一维。

    import pandas as pd
    import xarray as xr
    df = pd.read_csv(csv_file)
    xr = df.to_xarray()
    nc=xr.to_netcdf('my_netcdf.nc')

你能帮我解决这个问题吗?谢谢。

罗伯特·威尔逊

您需要首先将时间等设置为熊猫中的索引。因此,将您的代码修改为如下所示:

import pandas as pd
import xarray as xr
df = pd.read_csv(csv_file)
df = df.set_index(["time", "lon", "lat"]
xr = df.to_xarray()
nc=xr.to_netcdf('my_netcdf.nc')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章