在Python中获取列表的最小值的索引

时间:2019-01-11 10:47:28

标签: python pandas dataframe for-loop indexing

我有数据框dfSide0dfSide1,它们的行数不同但列相同:

  'distoperator' 'Name'         'camera_row'
0  67.3350       'End of Coil'  'a'
1  1331.4001     'Dent'         'b'
2  130.8350      'Oil'          'a'
3  859.2139      'Black Line'   'f'

我想要的是:

  1. 要获得dfSide0['distoperator']dfSide1['distoperator']的值之间所有可能的减法的最小值和绝对值,但仅当条件dfSide0['camera_row'] == dfSide1['camera_row']为True时。
  2. 要获取上述每个值的dfSide1['Name']值。

到目前为止,我的代码是:

temp_operator = []
temp_op_defect = []
k = -1
for i in dfSide0['distoperator']:
    k = k + 1
    j = dfSide0.index[k]
    c = min(abs(i - dfSide1['distoperator'][dfSide1['camera_row'] == dfSide0['camera_row'][j]]))
    h = dfSide1.index(min(abs(i - dfSide1['distoperator'][dfSide1['camera_row'] == dfSide0['camera_row'][j]])))
    s = dfSide1['Name'][h]
    temp_operator.append(c)
    temp_op_defect.append(s)

出现以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-37-e794dd1800c7> in <module>()
      6     j = dfSide0.index[k]
      7     c = min(abs(i - dfSide1['distoperator'][dfSide1['camera_row'] == dfSide0['camera_row'][j]]))
----> 8     h = dfSide1.index(min(abs(i - dfSide1['distoperator'][dfSide1['camera_row'] == dfSide0['camera_row'][j]])))
      9     s = dfSide1['Name'][h]
     10     temp_operator.append(c)

TypeError: 'Int64Index' object is not callable

有什么想法吗? 预先感谢您的输入。

1 个答案:

答案 0 :(得分:0)

我在为每个i创建的列表上使用了idxmin()方法:

buttonDelegate

似乎可行!