I have created a dataframe with a a column for codes and another for discrete values. I have a numpy array with some experimental values. How do i create a numpy array of codes that are associated to the nearest value defined in the dataframe.
The dataframe that defines the mapping between the codes and values doesn't have to be a dataframe. I am much more familiar with pandas than numpy, so i tend to lean towards using pandas dataframes. I am very unfamiliar with numpy so not sure what the best way to do this might be.
This is what i have tried and it gives me the correct response. Its just too slow. My actual data set is 500x1500 and i have over 700 sets of data that this operation needs to be performed over, so efficiency and speed are paramount. Ideas? Thoughts? Suggestions? Thanks!
import numpy as np
import pandas as pd
from pandas import DataFrame
def main():
npsize = (2,4)
#Create an array of data between -0.75 and 0.25
data = np.random.uniform(-0.75,0.25,npsize)
#Pandas dataframe that creates a map
codes = [np.array([1,2,3]),np.array([4,5,6]),np.array([7,8,9]),np.array([10,11,12]),np.array([13,14,15])]
values = [-0.75,-0.5,-0.25,0,0.25]
d = {'code':codes, 'value':values}
data_map = pd.DataFrame(data=d)
#I need to associate each element of data to the code within the data_map dataframe by looking up the nearest value
#For example ... -0.05 ---> [10,11,12]
#Silly Looping approach ... surely there is a better/faster way to do this!
mapped_data = np.zeros(shape=(2,4,3))
xctr = 0
yctr = 0
while xctr < npsize[0]:
#print(xctr)
while yctr < npsize[1]:
nearest_code = data_map.iloc[(data_map['value']-data[xctr,yctr]).abs().argsort()[:1]].code.iloc[0]
mapped_data[xctr,yctr] = nearest_code
yctr = yctr + 1
yctr = 0
xctr = xctr + 1
print (mapped_data)
if __name__ == "__main__":
main()

searchsortedmerge_asof