繁体   English   中英

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

[英]Getting the index of the minimum value of a list in python

我有数据帧dfSide0dfSide0dfSide1具有不同的行数但列相同:

  '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']是条件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

有任何想法吗? 预先感谢您的输入。

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

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

看来行得通!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM