简体   繁体   English

这是什么意思? xarray 错误:无法处理非唯一的多索引

[英]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_dataframedf.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 :您需要先删除索引中的重复条目,例如,如删除带有重复索引的熊猫行中所述:

  • The simplest choice would be to drop duplicates, eg, df[~df.index.duplicated()]最简单的选择是删除重复项,例如df[~df.index.duplicated()]
  • You might also use a groupby operation, eg, to compute the mean: df.groupby(level=df.index.names).mean()您也可以使用 groupby 操作,例如,计算平均值: 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.

相关问题 Pandas ValueError:尝试重新索引时无法处理非唯一多索引 - Pandas ValueError: cannot handle a non-unique multi-index when trying to re-index 需要处理具有非唯一多索引的串联数据帧 - Need to handle a concatenated dataframe with non-unique multi-index pandas.concat:无法处理非唯一的多索引! 熊猫蟒 - pandas.concat: Cannot handle a non-unique multi-index! Pandas Python 错误无法处理 groupby 子句中的非唯一多索引是什么意思? - What is the meaning of the error cannot handle a non-unique multi index in groupby clause? ValueError:无法处理非唯一的多索引。 添加列后使用 .loc 或 set_index 时 - ValueError: cannot handle a non-unique multi-index! when using .loc or .set_index after adding a column ValueError:无法处理非唯一的多索引! 尝试将来自多个数据帧的列合并到一个 dataframe - ValueError: cannot handle a non-unique multi-index! When trying to combine columns from multiple dataframes in to one dataframe 带有CSS样式属性错误的Pandas数据框(非唯一多索引)到html表 - Pandas dataframe (non-unique multi-index) to html table with CSS styling attribute error 如何合并/合并/加入 2 个具有非唯一多索引的数据帧以协调内容? - how to merge/concat/join 2 dataframes with a non-unique multi-index to reconcile the content? 获取 ValueError - 在 Pandas 中使用 expand() 时出现非唯一多索引 - Getting ValueError - non-unique multi-index when using explode() in Pandas ValueError:无法将具有非唯一 MultiIndex 的 DataFrame 转换为 xarray - ValueError: cannot convert a DataFrame with a non-unique MultiIndex into xarray
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM