簡體   English   中英

在 Python 中使用 xarray 的其他兩個變量擴展變量的坐標

[英]Expanding coordinates of a variable using other two variables of xarray in Python

假設我有 16 個網格單元格 (4 * 4) 的數據,每個單元格都有一個相應的索引 (0~15) 作為維度和坐標以及變量( a經度緯度)。 這是創建此數據的代碼。

import xarray as xr
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(16,3), \
                  columns=['a','longitude', 'latitude'], \
                  index=range(16))
ds = df.to_xarray()
ds

我想做的是:

使用每個單元格的經度緯度變量將數據a從 ( index ) 擴展到 ( longitude , latitude ) 的協調。
因此,生成的 DataSet 將包括經度緯度作為其維度和坐標以及(經度緯度)的變量a
如何在 xarray 功能中做到這一點?
謝謝!

這是解決該問題的一種方法:

# convert the index as a MultiIndex containing longitude and latitude
dat_2d = ds.set_index({'index': ['longitude', 'latitude']})

# unstack the MultiIndex
unstacked = dat_2d.unstack('index')

# plot
unstacked['a'].plot()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM