I am trying to convert a dataframe to 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:
cannot handle a non-unique multi-index!
Anybody know what is going on here?
To drop multiple levels from a multi-level column index, use the columns. droplevel() repeatedly.
From ndarray If data is an ndarray, index must be the same length as data. If no index is passed, one will be created having values [0, ..., len(data) - 1] . pandas supports non-unique index values. If an operation that does not support duplicate index values is attempted, an exception will be raised at that time.
In this article, we will discuss Multi-index for Pandas Dataframe and Groupby operations . Multi-index allows you to select more than one row and column in your index. It is a multi-level or hierarchical object for pandas object. Now there are various methods of multi-index that are used such as MultiIndex.
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.
You need to remove the duplicated entries in the index first, e.g., as described in Remove pandas rows with duplicate indices:
df[~df.index.duplicated()]
df.groupby(level=df.index.names).mean()
Once you've done this, you can safely convert the dataframe into xarray.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With