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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54028365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:15:07  来源:igfitidea点击:

what does this mean? xarray error: cannot handle a non-unique multi-index

pythonpandaspython-xarray

提问by Y. Peng

I am trying to convert a dataframe to xarray. The head is like this:

我正在尝试将数据帧转换为 xarray。头部是这样的:

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, or 时df.to_xarray,我收到以下错误消息:

cannot handle a non-unique multi-index!

cannot handle a non-unique multi-index!

Anybody know what is going on here?

有人知道这里发生了什么吗?

回答by shoyer

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, e.g., as described in Remove rows with duplicate indices (Pandas DataFrame and TimeSeries):

您需要先删除索引中的重复条目,例如,如删除具有重复索引的行(Pandas DataFrame 和 TimeSeries)中所述

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

Once you've done this, you can safely convert the dataframe into xarray.

完成此操作后,您可以安全地将数据帧转换为 xarray。

回答by Hossein Madadi

When you convert csv to netcdf through to_xarray, It is important the arrangement of heads are equal to the arrangement in your cod, otherwise you get the error: cannot handle a non-unique multi-index.

当您通过 将 csv 转换为 netcdf 时to_xarray,头部的排列与 cod 中的排列相同很重要,否则您将获得error: cannot handle a non-unique multi-index.

回答by ??????? ????

In this case df.columns.is_uniquewould return False. To identify which one is repeating you can see the frequency of each column pair by df.columns.value_counts(). For multiindexing to work it should show 1for all tuples.

在这种情况下df.columns.is_unique会返回False。要确定哪个是重复的,您可以通过 来查看每个列对的频率df.columns.value_counts()。要使多索引工作,它应该显示1所有元组。