【问题标题】:Error"Can only compare identically-labeled Series objects" and sort_index错误“只能比较标记相同的系列对象”和 sort_index
【发布时间】:2017-11-30 01:53:15
【问题描述】:

我有两个数据框 df1 df2 具有相同的行数和列数以及变量,我正在尝试比较两个数据框中的布尔变量 choice。然后使用if/else 来操作数据。但是当我尝试比较布尔变量时似乎有些错误。

这是我的数据框示例和代码:

#df1
v_100     choice #boolean
7          True
0          True
7          False
2          True

#df2
v_100     choice #boolean
1          False
2          True
74         True
6          True

def lastTwoTrials_outcome():
     df1 = df.iloc[5::6, :] #df1 and df2 are extracted from the same dataframe first
     df2 = df.iloc[4::6, :]

     if df1['choice'] != df2['choice']:  # if "choice" is different in the two dataframes
         df1['v_100'] = (df1['choice'] + df2['choice']) * 0.5

这是错误:

if df1['choice'] != df2['choice']:
File "path", line 818, in wrapper
raise ValueError(msg)
ValueError: Can only compare identically-labeled Series objects

我发现了同样的错误here,答案首先建议sort_index,但我真的不明白为什么?谁能详细解释一下(如果这是正确的解决方案)?

谢谢!

【问题讨论】:

    标签: python pandas indexing boolean


    【解决方案1】:

    我认为您需要reset_index 来获取相同的索引值,然后再使用comapare - 创建新列最好使用masknumpy.where

    另外+ 使用| 因为使用布尔值。

    df1 = df1.reset_index(drop=True)
    df2 = df2.reset_index(drop=True)
    df1['v_100'] = df1['choice'].mask(df1['choice'] != df2['choice'],
                                      (df1['choice'] + df2['choice']) * 0.5)
    
    
    df1['v_100'] = np.where(df1['choice'] != df2['choice'],
                           (df1['choice'] | df2['choice']) * 0.5,
                            df1['choice'])
    

    样品:

    print (df1)
       v_100  choice
    5      7    True
    6      0    True
    7      7   False
    8      2    True
    
    print (df2)
       v_100  choice
    4      1   False
    5      2    True
    6     74    True
    7      6    True
    

    df1 = df1.reset_index(drop=True)
    df2 = df2.reset_index(drop=True)
    print (df1)
       v_100  choice
    0      7    True
    1      0    True
    2      7   False
    3      2    True
    
    print (df2)
       v_100  choice
    0      1   False
    1      2    True
    2     74    True
    3      6    True
    
    df1['v_100'] = df1['choice'].mask(df1['choice'] != df2['choice'],
                                      (df1['choice'] | df2['choice']) * 0.5)
    
    print (df1)
       v_100  choice
    0    0.5    True
    1    1.0    True
    2    0.5   False
    3    1.0    True
    

    【讨论】:

    • 感谢您的帮助!我已经尝试了这两种方法,但仍然出现确切的错误。
    • print (df1.index)print (df2.index) 是什么? print (len(df1.index))print (len(df2.index)) 是什么?
    • print (df1.index) : RangeIndex(start=5, stop=2160, step=6) print (df2.index) : RangeIndex(start=4, stop=2160, step=6) print (len(df1.index)) : 360 @ 987654339@:360。谢谢!
    • 所以问题是索引不同 - start=5start=4 不同。你试试 reset_index 吗?
    • 是的,打印在reset_index 之后。那么为什么他们仍然有不同的开始呢?我重新检查了它们的行数和列数是否相同。
    【解决方案2】:

    发生错误是因为您比较了具有不同索引的两个 pandas.Series 对象。一个简单的解决方案是仅比较系列中的值。试试看:

    if df1['choice'].values != df2['choice'].values
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-17
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      相关资源
      最近更新 更多