I apply some processing, such as replacing a matrix element from one matrix index value to another. It works great.
ds1 = [[ 4, 13, 6, 9], [ 7, 12, 5, 7], [ 7, 0, 4, 22], [ 9, 8, 12, 0]] ds2 = [[ 4, 1], [ 5, 3], [ 6, 1], [ 7, 2], [ 4, 1 ], [ 8, 2], [ 9, 3], [12, 1], [13, 2], [22, 3]] ds1= pd.DataFrame(ds1) ds2= pd.DataFrame(ds2) #Processing ds1 by replacing print type(ds2) ds2 = ds2.groupby(0).mean() #.........X print type(ds2) C = np.where(ds1.values.ravel()[:, None] == ds2.values[:, 0]) ds1_new = ds1.values.ravel() ds1_new[C[0]]=ds2.values[C[1], 1] #when I comment line x, it works.Otherwise getting error on this line ds1_new = ds1_new.reshape(4,4)
The reason for using ds2 = ds2.groupby(0).mean() is to get the average value of similar elements. When I uncomment it, it works without errors.
Version
Python 2.7.3 numpy - 1.9.2 pandas - 0.15.2
Edit
My main goal is to compare the index value with ds2 in ds1 and replace it with the corresponding value, so the result will look like
ds1_new = [[ 1, 2, 1, 3], [ 2, 1, 3, 2], [ 2, 0, 1, 3], [ 3, 2, 1, 0]]