如何使用列的值在 ndarray 上合并 2 个 numpy ndarray?
How to merge 2 numpy ndarray on a ndarray using values of a column?
我有 2 个数组:
a = np.array([[1,2], [5,0], [6,4]])
b = np.array([[1,10],[6,30], [5,20]])
我希望将它们合并成一个数组,如下所示:
[[ 1 2 10]
[ 5 0 20]
[ 6 4 30]]
有人知道按第 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)
您可以使用 numpy.searchsorted
:
c = np.c_[a, b[np.searchsorted(a[:, 0], b[:, 0]), 1]]
print(c)
array([[ 1, 2, 10],
[ 5, 0, 20],
[ 6, 4, 30]])
分解一下,注意应用于 b
的行索引检索 b[:, 0]
中每个值的 a[:, 0]
的索引:
print(np.searchsorted(a[:, 0], b[:, 0]))
[0 2 1]
我找到了 pandas 的替代解决方案,效率低于 numpy,但我希望 post 它也是,因为我认为这很有启发性。
给我的好解jpp(我不知道那个方法),有一个限制,a和b必须有相同的键。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
def merge_w_np(a, b):
zeros = np.zeros((a.shape[0], np.shape(b)[1] -1), dtype=int)
a = np.append(a, zeros, axis=1)
l1 = a[:,0].tolist()
for j, i in enumerate(b[:,0].tolist()):
a[l1.index(i),2] = b[j,1]
print(a)
def merge_w_pd(a, b):
dfa = pd.DataFrame(data=a, # values
index=a[:,0]) # 1st column as index
dfb = pd.DataFrame(data=b, # values
index=b[:,0]) # 1st column as index
dfa.columns = ['id', 'value']
dfb.columns = ['id', 'value']
# print('a',dfa)
# print('b',dfb)
dfc = dfa.merge(dfb, left_on='id', right_on='id', how='outer')
print(dfc)
a = np.array([[1,2], [2,8], [5,0], [6,4], [7,9]])
b = np.array([[1,10],[6,30], [5,20]])
merge_w_np(a, b)
merge_w_pd(a, b)
我有 2 个数组:
a = np.array([[1,2], [5,0], [6,4]])
b = np.array([[1,10],[6,30], [5,20]])
我希望将它们合并成一个数组,如下所示:
[[ 1 2 10]
[ 5 0 20]
[ 6 4 30]]
有人知道按第 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)
您可以使用 numpy.searchsorted
:
c = np.c_[a, b[np.searchsorted(a[:, 0], b[:, 0]), 1]]
print(c)
array([[ 1, 2, 10],
[ 5, 0, 20],
[ 6, 4, 30]])
分解一下,注意应用于 b
的行索引检索 b[:, 0]
中每个值的 a[:, 0]
的索引:
print(np.searchsorted(a[:, 0], b[:, 0]))
[0 2 1]
我找到了 pandas 的替代解决方案,效率低于 numpy,但我希望 post 它也是,因为我认为这很有启发性。 给我的好解jpp(我不知道那个方法),有一个限制,a和b必须有相同的键。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
def merge_w_np(a, b):
zeros = np.zeros((a.shape[0], np.shape(b)[1] -1), dtype=int)
a = np.append(a, zeros, axis=1)
l1 = a[:,0].tolist()
for j, i in enumerate(b[:,0].tolist()):
a[l1.index(i),2] = b[j,1]
print(a)
def merge_w_pd(a, b):
dfa = pd.DataFrame(data=a, # values
index=a[:,0]) # 1st column as index
dfb = pd.DataFrame(data=b, # values
index=b[:,0]) # 1st column as index
dfa.columns = ['id', 'value']
dfb.columns = ['id', 'value']
# print('a',dfa)
# print('b',dfb)
dfc = dfa.merge(dfb, left_on='id', right_on='id', how='outer')
print(dfc)
a = np.array([[1,2], [2,8], [5,0], [6,4], [7,9]])
b = np.array([[1,10],[6,30], [5,20]])
merge_w_np(a, b)
merge_w_pd(a, b)