[英]what does this mean? xarray error: cannot handle a non-unique multi-index
I am trying to convert a dataframe to xarray.我正在尝试将数据帧转换为 xarray。 The head is like this:
头部是这样的:
z Class DA x y iline xline idz
2 651 289 1455.0 2.0 0.62239 2345322.0 76720.0
290 1460.0 0.0 0.46037 2345322.0 76720.0
291 1465.0 4.0 0.41280 2345322.0 76720.0
292 1470.0 0.0 0.39540 2345322.0 76720.0
293 1475.0 2.0 0.61809 2345322.0 76720.0
when I use xr.DataSet.from_dataframe
, or df.to_xarray
, I got the following error message:当我使用
xr.DataSet.from_dataframe
或df.to_xarray
,我收到以下错误消息:
cannot handle a non-unique multi-index!
Anybody know what is going on here?有人知道这里发生了什么吗?
The multi-index of your data frame has duplicate entries, which xarray cannot unstack into a multi-dimensional array -- the elements of the hypothetical arrays would not have unique values.数据框的多索引有重复的条目,xarray 无法将其拆分为多维数组——假设数组的元素不会具有唯一值。
You need to remove the duplicated entries in the index first, eg, as described in Remove pandas rows with duplicate indices :您需要先删除索引中的重复条目,例如,如删除带有重复索引的熊猫行中所述:
df[~df.index.duplicated()]
df[~df.index.duplicated()]
df.groupby(level=df.index.names).mean()
df.groupby(level=df.index.names).mean()
Once you've done this, you can safely convert the dataframe into xarray.完成此操作后,您可以安全地将数据帧转换为 xarray。
In this case df.columns.is_unique
would return False
.在这种情况下,
df.columns.is_unique
将返回False
。 To identify which one is repeating you can see the frequency of each column pair by df.columns.value_counts()
.要确定哪个是重复的,您可以通过
df.columns.value_counts()
查看每一列对的频率。 For multiindexing to work it should show 1
for all tuples.要使多索引工作,它应该为所有元组显示
1
。
当您通过to_xarray
将 csv 转换为 netcdf 时,头部的排列与 cod 中的排列相同很重要,否则会出现error: cannot handle a non-unique multi-index
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.