I am applying some processing like replacing matrix element from one matrix index value to another. it works fine.
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)
Reason behind using ds2 = ds2.groupby(0).mean()
is getting average value of similar elements. When I uncomment it, it works without error.
Version
Python 2.7.3
numpy - 1.9.2
pandas - 0.15.2
Edit
My main goal is to match the index value from ds2
into ds1
and replace it with corresponding value, so the output would look like
ds1_new = [[ 1, 2, 1, 3],
[ 2, 1, 3, 2],
[ 2, 0, 1, 3],
[ 3, 2, 1, 0]]