python 2.7 - compare two numpy arrays by first column in sorted order and create new array -
i have 2 numpy arrays , b. want compare 2 numpy array column 0 contains time series data.i want create new numpy array sorted time series in column 0 , associated values time. , if no values found insert null data. example
= np.array([[0.002, 0.998], [0.004, 0.997], [0.006, 0.996], [0.008, 0.995], [0.010, 0.993]]) b = np.array([[0.002, 0.666], [0.004, 0.665], [0.0041, 0.664], [0.0041,0.663], [0.0042, 0.664], [0.0043, 0.664], [0.0044, 0.663], [0.0045, 0.663], [0.005, 0.663], [0.006, 0.663], [0.0061, 0.662], [0.008, 0.661]]) expected ouput c= [[0.002, 0.998, 0.666], [0.004, 0.997, 0.665], [0.0041, null, 0.664], [0.0041, null ,0.663], [0.0042, null, 0.664], [0.0043, null, 0.664], [0.0044, null, 0.663], [0.0045, null, 0.663], [0.005, null, 0.663], [0.006, 0.996, 0.663], [0.0061, null, 0.662], [0.008, 0.995, 0.661], [0.010, 0.993, null]]
any tricks this...
there might easier way approach works:
# rows common first columns c1 = [(x, a[a[:,0]==x][0,1], b[i,1]) i,x in enumerate(b[:,0]) if x in a[:,0]] # rows of not in b c2 = [(x, a[i,1], np.nan) i,x in enumerate(a[:,0]) if x not in b[:,0]] # rows of b not in c3 = [(x, np.nan, b[i,1]) i,x in enumerate(b[:,0]) if x not in a[:,0]] # combine , sort c4=np.vstack((c1,c2,c3)) c5=c4[c4[:,0].argsort()] print c5
Comments
Post a Comment