python - 检查两个索引内列中数据框中的重复值
问题描述
我有一个数据框,如 excel 文件中所示
我想根据 id 查找重复值,例如nn_id列中ID 0 and ID 1
的值不相同,因此我们进一步检查nn_id列中 的值是否相同,因此如果值相同,它们会打印 nn_id 列中的值,切片-0010-EDSR_x2_X和slice-0010-EDSR_x2_Y
所以输出将是字典的形式进一步检查哪些值不相同,所以什么也不做。366 393
ID 2 and ID 3
595 595
{595:[(492,260),(491,248)]}
ID 4 and ID 5
458 486
如果它令人困惑,我很抱歉,但我想检查两个 ID 的nn_id值是否相同,然后制作一个相邻列值的字典。
解决方案
这是否实现了您的目标?可能有更优雅的方法来实现相同的目标。我假设你DataFrame
df
的桌子上有一个。
df_shift = df.shift(1) # shift database with 1 row
same_idx = df['nn_id'] == df_shift['nn_id']
# get column positions for columns of interest
col1_pos = df.columns.get_loc('slice-0010-EDSR_x2_X ')
col2_pos = df.columns.get_loc('slice-0010-EDSR_x2_Y')
nn_idx_pos = df.columns.get_loc('nn_id')
my_dict = {} # define empty dict to store your results.
for i in np.where(same_idx)[0]: # for each row where the nn_idx value is the same
# define the value that you're after
my_value = [(df.iloc[i-1, col1_pos], df.iloc[i-1, col2_pos]),
(df.iloc[i, col1_pos], df.iloc[i, col2_pos])]
# and add element to dictionary
my_dict[df.iloc[i, nn_idx_pos]] = my_value
推荐阅读
- c++ - 在 XCode 中构建 SONY CameraRemote SDK 示例应用程序时出现 Permission denied 错误
- javascript - 从嵌套的 json 中过滤项目
- powerbi - 带有财政日历的 MTD 预算
- sql-server - SQL Server 中 varbinary(8000) 和 varbinary(max) 之间的技术区别?
- listview - Xamarin Forms:HorizontalOptions 属性的对齐问题
- javascript - 如何在一个 Vue 组件中处理多个 Firebase 集合?
- r - purrr:将列名作为参数列表移交给 pmap()
- java - java.lang.NoClassDefFoundError: org/apache/commons/text/StringEscapeUtils
- node.js - 护照未显示在会话 socket.io 中
- angular - .NET Core 5 角 SSR