熊猫错误“只能比较标记相同的DataFrame对象”

时间:2020-03-06 01:34:26

标签: python pandas pandas-groupby

我有2个数据帧生成为

df_atn5_agg = df_atn5.groupby(['pipeline_name'], as_index=False).agg({'tot_map_comp_mins':['count', p25]})
df2_t1 = df_atn5_agg[df_atn5_agg['tot_map_comp_mins']['count'] > 1]

df_prod_agg = df_prod.groupby(['pipeline_name'], as_index=False).agg({'tot_map_comp_mins':['count', p25]})
df3_prod = df_prod_agg[df_prod_agg['tot_map_comp_mins']['count'] > 1]

我想在

中添加一个新列
df2_t1['exchange_ratio'] = (
                            (df2_t1['tot_map_comp_mins']['p25']* 1.0) / 
                            ( 
                                df_prod_agg[df2_t1['pipeline_name'] == df_prod_agg['pipeline_name']]['tot_map_comp_mins']['p25']
                            )
                           )

我明白了

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-298-ddbc217187e3> in <module>
      1 df2_t1['exchange_ratio'] = ((df2_t1['tot_map_comp_mins']['p25']* 1.0) / 
      2                             ( 
----> 3                                 df_prod_agg[df2_t1['pipeline_name'] == df_prod_agg['pipeline_name']]
      4                                 ['tot_map_comp_mins']['p25']
      5                             )

/mnt/xarfuse/uid-115541/4cee94fa-ns-4026531840/pandas/core/ops/common.py in new_method(self, other)
     62         other = item_from_zerodim(other)
     63 
---> 64         return method(self, other)
     65 
     66     return new_method

/mnt/xarfuse/uid-115541/4cee94fa-ns-4026531840/pandas/core/ops/__init__.py in wrapper(self, other)
    519 
    520         if isinstance(other, ABCSeries) and not self._indexed_same(other):
--> 521             raise ValueError("Can only compare identically-labeled Series objects")
    522 
    523         lvalues = extract_array(self, extract_numpy=True)

ValueError: Can only compare identically-labeled Series objects

基本上我想访问['tot_map_comp_mins']['p25']所在行df_prod_agg的{​​{1}}

我们非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

索引可能存在问题,例如: Pandas "Can only compare identically-labeled DataFrame objects" error

您尝试过吗:

df2_t1['pipeline_name'].reset_index(drop=True) == df_prod_agg['pipeline_name'].reset_index(drop=True)