将netCDF文件导入到Pandas数据框 [英] Import netCDF file to Pandas dataframe

查看:152
本文介绍了将netCDF文件导入到Pandas数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

圣诞快乐!我对Python和Pandas还是很陌生,因此可以提供任何帮助.我正在尝试读取netCDF文件,可以这样做,然后将其导入到Pandas Dataframe中. netcDF文件是2D格式的,因此我只想将其转储".我尝试了DataFrame方法,但无法识别该对象.大概我需要将netCDF对象转换为2D numpy数组吗?再次感谢您提供有关最佳方法的想法.

Merry Christmas! I am still very new to Python and Pandas, so any help is appreciated. I am trying to read in a netCDF file, which I can do and then import that into a Pandas Dataframe. The netcDF file is 2D so I just want to 'dump it in'. I have tried the DataFrame method but it doesn't recognize the object. Presumably I need to convert the netCDF object to a 2D numpy array? Again, thanks for any ideas on the best way to do this.

推荐答案

xarray 库可处理任意维netCDF数据,并保留元数据. Xarray提供了一种简单的方法来打开netCDF文件,并将其转换为熊猫数据帧:

The xarray library handles arbitrary-dimensional netCDF data, and retains metadata. Xarray provides a simple method of opening netCDF files, and converting them to pandas dataframes:

import xarray as xr

ds = xr.open_dataset('/path/to/netcdf')
df = ds.to_dataframe()

这将创建一个具有多索引的数据框,其中包含所有维.不幸的是,Pandas不支持任意元数据,因此在转换中会丢失,但是您可以保留ds并使用其中的元数据.

This will create a dataframe with a multi-index with all of the dimensions in it. Unfortunately, Pandas doesn't support arbitrary metadata, so that will be lost in the conversion, but you can keep the ds around, and use the metadata from that.

这篇关于将netCDF文件导入到Pandas数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆