【问题标题】:Trying to get the i+1 index on pandas dataframe failling试图在 pandas 数据帧上获取 i+1 索引失败
【发布时间】:2022-01-18 13:41:25
【问题描述】:

我正在尝试循环遍历数据帧以比较 i 和 i+1 索引,如下所示:

d = {'col1': [1, 2,0,55,12,1, 3,1,56,13], 'col2': [3,4,44,34,46,2,3,43,35,47], 'col3': ['A','A','A','B','B','A','B','B','B','B'] } 
df = pd.DataFrame(data=d)
df

for index, row in df.iterrows():
    if df.at[index,"col3"] != df.at[index+1,"col3"]:
        print('True')
    else:
        print("false")

我收到此错误:

false
false
True
false
True
True
false
false
false

KeyError Traceback(最近 最后打电话) 在 () 3 4 用于索引,df.iterrows() 中的行: ----> 5 如果 df.at[index,"col3"] != df.at[index+1,"col3"]: 6 打印('真') 其他 7 个:

in getitem(自我,钥匙) 2140 第2141章 -> 2142 返回 self.obj._get_value(*key, takeable=self._takeable) 2143 2144 def setitem(自我、键、值):

   2538         try:
-> 2539             return engine.get_value(series._values, index)
   2540         except (TypeError, ValueError):
   2541 

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 10

【问题讨论】:

    标签: python pandas indexing keyerror


    【解决方案1】:

    您的代码将始终在最后一行失败,因为您试图获取末尾之后的行。

    通常,在使用两个不同大小的列表进行这种迭代时,zip 函数是最好的解决方案:

    for this_row, next_row in zip(df["col3"], df["col3"][1:]):
        if this_row != next_row:
            print('True')
        else:
            print("false")
    

    请注意,即使您的数据框只有一个元素,此代码也不会引发异常。

    如果更喜欢使用索引进行迭代,另一种选择是:

    for this_index, next_index in zip(df.index, df.index[1:]):
        if df.at[this_index,"col3"] != df.at[next_index,"col3"]:
            print('True')
        else:
            print("false")
    

    【讨论】:

    • 谢谢您,先生,但是如果我想遍历整个 df 怎么办?通过不使用 df["col3"], df["col3"][1:] ?因为我稍后需要使用“索引”
    • 我在答案中包含了该选项。
    【解决方案2】:

    下面是我将如何做到的。它可能不是最佳解决方案,但它应该可以帮助您解决问题。

    关于pandas为什么会抛出异常,请看下面的注释。

    我把它作为一个函数的原因是你以后可以为不同的数据帧/任务重用相同的函数。

    另外,我个人的习惯是在迭代dataframe时,不需要value,我会避免使用iter_row方法(这种方法计算量很大,尤其是在处理大数据时。但这只是基于我的个人经验)。

    我希望看到其他人的其他出色解决方案!

    def identify_same_or_not(data=None,col_index=None):
        ### 1: holder is the final result from comparation
        holder = []
        ### 2: Since we are only interested in row index, iter_row might not needed
        # Since we trying to compare x with x + 1, we need set the index loop as range(len(length_of_data) - 1)
        # otherwise, in the final iteration (based on the example you provided), pd will try to compare row 9 with row 10,
        # However, Row 10 does not exist in df; therefore, pd will throw exception
        for row_index in range(len(df)-1):
        # Same logic as you provided
          if data.iloc[row_index,col_index] != df.iloc[row_index + 1,col_index]:
            holder.append(True)
          else: 
            holder.append(False)
        return holder
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      • 1970-01-01
      • 2023-03-05
      相关资源
      最近更新 更多