1
我有一個數據幀net
,其中包含兩個位置A
和B
之間的距離d
。Python:如何映射矩陣和數據框之間的值?
net =
A B d
0 5 3 3.5
1 2 0 2.3
2 3 2 1.2
3 4 5 2.2
4 0 1 3.2
5 0 3 4.5
然後,我有一個包含兩個對之間所有可能的距離的對稱矩陣M
,所以:
M =
0 1 2 3 4 5
0 0 3.2 2.3 4.5 1.7 5.2
1 3.2 0 2.1 0.7 3.9 3.8
2 2.3 2.1 0 1.2 1.5 4.7
3 4.5 0.7 1.2 0 3.2 3.5
4 1.7 3.9 1.5 3.2 0 2.2
5 5.2 3.8 4.7 3.5 2.2 0
我要生成一個包含兩個隨機不同位置的新數據框中df1
A
和B
在相同的距離區間ds > np.floor(d) & ds < np.floor(d)+1
。
這是我在做什麼
H = []
W = []
for i in net.index:
tmp = net['d'][i]
ds = np.where((M > np.floor(tmp)) & (M < np.floor(tmp)+1))
size = len(ds[0])
ind = randint(size) ## find two random locations with distance ds
h = ds[0][ind]
w = ds[1][ind]
H.append(h)
W.append(w)
df1 = pd.DataFrame()
df1['A'] = H
df1['B'] = W
我得到了一個錯誤'AttributeError的: 'numpy.ndarray'對象沒有屬性'vstack' – emax