【发布时间】:2021-12-31 10:34:36
【问题描述】:
我在 python 中构建了一个三维 xarray
import numpy as np
import pandas as pd
import xarray as xr
data_np = np.array([
[ [1, 2, 0, 8], [3, 4, 11, 2], [5, 6, 43, 90] ],
[ [7, 8, 2, 66], [9, 10, 31, 21], [11, 12, 56, 45] ]
])
dims = ['patient', 'parameter', 'day']
coords = {'patient':['Joe','Bill'], 'parameter':['a', 'b', 'c'], 'day':[0, 1, 2, 3]}
data = xr.DataArray(data_np, dims = dims, coords = coords )
我想将它导出到具有这种结构的 excel 工作表中:
使用 Pandas 数据框,我可以获得所有变量(dims)沿同一轴分支的 excel 表:
df = data.to_dataframe('value')
df = df.transpose()
with pd.ExcelWriter("main.xlsx") as writer:
df.to_excel(writer)
我怎样才能得到想要的结构?
【问题讨论】:
标签: python excel pandas python-xarray