Search code examples
pythonlistindexingknneuclidean-distance

How to get the index of multiple mininum values in a column?


I'm trying to get the k minimun values of each column on a dataframe. however I am unable to get the indices. I have a dataframe that contains the distances between data called ddist. I want to select the k minimum elements in each column and make a list of the no. of row each element is in so I can use it for KNN later.

These are the lines where I tried to get the indices, however i get a list with the indexes and the values and cannot access the indices alone.

list_min = []
for column in ddist:
    idx = ddist.nsmallest(k,column)
    idx=idx[0]
    list_min.append(idx)
list_min

the output looks like this

[43     0.0
 188    0.0
 202    0.0
 Name: 0, dtype: float64,
 21    0.04
 26    0.04
 94    0.04
 Name: 0, dtype: float64,
 27     0.39
 52     0.39
 164    0.39
 Name: 0, dtype: float64,
 33     0.01
 131    0.01
 210    0.01
...

but I only want to get the integers that look appear like a column on each entry.


Solution

  • You should try pd.Series.index:

    list_min = []
    for column in ddist:
        idx = ddist.nsmallest(k,column)
        idx=idx[0]
        list_min.append(idx.index.tolist())
    list_min