我有2个月:
a = np.array([[1,2], [5,0], [6,4]])
b = np.array([[1,10],[6,30], [5,20]])
我希望将它们合并成一个数组,如下所示:
^{pr2}$有人知道用列0的值合并2数组的非迭代模式吗?在
我发现只有这样:
import numpy as np
a = np.array([[1,2], [5,0], [6,4]])
b = np.array([[1,10],[6,30], [5,20]])
new0col = np.zeros((a.shape[0],1), dtype=int)
a = np.append(a, new0col, axis=1)
l1 = a[:,0].tolist()
l2 = b[:,0].tolist()
for i in l2:
a[l1.index(i),2] = b[l2.index(i),1]
print(a)
您可以使用^{} :
请注意,应用于}的索引:
^{pr2}$b
的行索引为b[:, 0]
中的每个值检索{我发现了另一种解决方法,用熊猫来解决问题,效率不如小熊猫,但我也希望贴出来,因为我认为这很有启发性。 给我jpp(我不知道这个方法)的好的解决方案有一个限制,a和b必须有相同的键。在
相关问题 更多 >
编程相关推荐