首页 > 解决方案 > 我们如何根据级别进行熊猫系列比较?

问题描述

我有一个多索引数据框y_test

blockNumber_base10@coarsegrained#1024  address_idx
9441280                                2             -0.012421
                                       3              0.001400
                                       6             -0.012696
                                       7              0.003558
                                       15             0.003649
                                                        ...   
9854976                                935783         0.000000
                                       935785         0.000000
                                       935786         0.000000
                                       935788         0.000000
                                       935789         0.000000
Name: price@fwd#001024@div#price#m1, Length: 1146272, dtype: float64

我们想与 groupby min 进行比较。

y_test_tmp  = y_test.groupby(level = 0).quantile(0.8)

blockNumber_base10@coarsegrained#1024
9441280    0.005130
9442304    0.000973
9443328    0.000461
9444352    0.001354
9445376    0.000921
             ...   
9850880    0.000552
9851904    0.000953
9852928    0.000591
9853952    0.000000
9854976    0.000000
Name: price@fwd#001024@div#price#m1, Length: 405, dtype: float64

我想根据它们与它们进行比较blockNumber_base10@coarsegrained#1024,所以我想计算 y_test > y_test_tmp。但我得到了这个错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-318-ad2813982941> in <module>
----> 1 y_test > y_test_tmp

~/anaconda3/lib/python3.8/site-packages/pandas/core/ops/common.py in new_method(self, other)
     63         other = item_from_zerodim(other)
     64 
---> 65         return method(self, other)
     66 
     67     return new_method

~/anaconda3/lib/python3.8/site-packages/pandas/core/arraylike.py in __gt__(self, other)
     43     @unpack_zerodim_and_defer("__gt__")
     44     def __gt__(self, other):
---> 45         return self._cmp_method(other, operator.gt)
     46 
     47     @unpack_zerodim_and_defer("__ge__")

~/anaconda3/lib/python3.8/site-packages/pandas/core/series.py in _cmp_method(self, other, op)
   4971 
   4972         if isinstance(other, Series) and not self._indexed_same(other):
-> 4973             raise ValueError("Can only compare identically-labeled Series objects")
   4974 
   4975         lvalues = extract_array(self, extract_numpy=True)

ValueError: Can only compare identically-labeled Series objects

有什么建议吗?谢谢!

标签: pandas

解决方案


推荐阅读