1. Finding the minimum value in column "2" and writing the index to res. "2" ==> "4"
  2. Search for the minimum value in the column that was found in the previous paragraph and the index entry in res, excluding the results available in res. "4" ==> "1".
  3. Search for the minimum value in the column that was found in the previous paragraph and the index entry in res, excluding the results available in res. "1" ==> "3".
  4. Search for the minimum value in the column that was found in the previous paragraph and the index entry in res, excluding the results available in res. "3" ==> "5".

    1 2 3 4 5 1 inf 5.909091 8.636364 7.272727 4.454545 2 7.222222 inf 8.666667 7.666667 1.777778 3 15.833333 13.000000 inf 9.166667 14.666667 4 4.444444 3.833333 3.055556 inf 4.833333 5 24.500000 8.000000 44.000000 43.500000 inf 

    An example below. But the function could not be applied

     res = ['2'] def f(): i = 0 ret = d[res[i]].idxmin() i += 1 if ret not in res: res.append(ret) print(res) x = d.apply(f) 

The output should be: [2, 4, 1, 3, 5]

    1 answer 1

     def f(col): ret = col.loc[~col.index.isin(res)].idxmin() if ret not in res: res.append(ret) res = [2] _ = df.iloc[:, 1:].apply(f) print(res) 

    Result:

     [2, 4, 1, 3, 5]