对多行 Dataframe 应用自定义函数

2guxujil  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(332)

我目前正在开发一个函数,该函数将根据多个条件(平方米、图像和价格)检测行是否重复。它工作得非常好,直到找到重复项,从 Dataframe 中删除该行,然后我的for循环被打乱。这就产生了 IndexError: single positional indexer is out-of-bounds .

  1. def image_duplicate(df):
  2. # Detecting duplicates based on the publications' images, m2 and price.
  3. for index1 in df.index:
  4. for index2 in df.index:
  5. if index1 == index2:
  6. continue
  7. print('index1: {} \t index2: {}'.format(index1, index2))
  8. img1 = Image.open(requests.get(df['img_url'].iloc[index1], stream=True).raw).resize((213, 160))
  9. img2 = Image.open(requests.get(df['img_url'].iloc[index2], stream=True).raw).resize((213, 160))
  10. img1 = np.array(img1).astype(float)
  11. img2 = np.array(img2).astype(float)
  12. ssim_result = ssim(img1, img2, multichannel=True)
  13. ssim_result_percentage = (1+ssim_result)/2
  14. if ssim_result_percentage > 0.80 and df['m2'].iloc[index1] == df['m2'].iloc[index2] \
  15. and df['Price'].iloc[index1] == df['Price'].iloc[index2]:
  16. df.drop(df.iloc[index2], inplace=True).reindex()
  17. image_duplicate(full_df)

解决这个问题的好办法是什么?
编辑:示例:

预期输出:从数据框中删除一行[2]。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题