how to get index of closets match elements in list

Viewed 63

I have two lists that are related to one another. An original list and a downsampled list. The original list contains all of my reference data and the downsampled list contains a small sample of the original data. They look like this:

original list:

original = [
 0.0,
 0.033,
 0.066,
 0.1,
 0.133,
 0.166,
 0.199,
 0.233,
 0.266,
 0.299,
 0.333,
 0.366,
 0.399,
 0.433,
 0.466,
 0.499,
 0.533,
 0.566,
 0.599,
 0.633,
 0.666,
 0.699,
 0.733,
 0.766,
 0.799,
 0.833,
 0.866,
 0.899,
 0.933,
 0.966,
 0.999,
 1.033,
 1.066,
 1.099,
 1.133,
 1.166,
 1.199,
 1.232,
 1.266,
 1.299,
 1.332,
 1.366,
 1.399,
 1.432,
 1.466,
 1.499,
 1.532,
 1.566,
 1.599,
 1.632,
 1.666,
 1.699,
 1.732,
 1.766,
 1.799,
 1.832,
 1.866,
 1.899,
 1.932,
 1.966,
 1.999,
 2.032,
 2.066,
 2.099,
 2.132,
 2.166,
 2.199,
 2.232,
 2.266,
 2.299,
 2.332,
 2.366,
 2.399,
 2.432,
 2.466,
 2.499,
 2.532,
 2.566,
 2.599,
 2.632,
 2.666,
 2.699
]

and the downsampled list:

downsampled = [
0.633078,
1.632956,
2.632756
]

How do I get the indices of the elements in original that are exactly or the closet match to the items in downsampled?

2 Answers

With numpy's help, using abs and argmin:

import numpy as np
def dist(d):
    return abs(d - np.array(original))
[np.argmin(dist(d)) for d in downsampled]

output:

[19, 49, 79]

As an alternative solution, I suggest an approach using only built-in pandas methods.

You just have to create dataframes from the lists

df_original = pd.DataFrame(original, columns=['original_value']).reset_index().rename(columns={'index': 'original_index'})
df_downsampled = pd.DataFrame(downsampled, columns=['downsampled_value']).reset_index().rename(columns={'index': 'downsampled_index'})

Then you just perform a left join from original into downsampled with pandas merge_asof

result = pd.merge_asof(df_downsampled, df_original, left_on=['downsampled_value'], right_on=['original_value'])
result['original_index'].to_list()
[19, 49, 79]

I like this approach because you end up with the complete merged dataframe that you can use to look at the selected values directly and perform other operations.

Related