使用numpy逐行连接不同大小的数组

2024-05-19 05:06:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我设法用np.连接. 在

combined = np.concatenate((price1,price2))

如何使用numpy将两个不同大小的csv数据集(除了其中一个数据集有一个额外的列外,它们包含公共的头)按行联接?在

数据集1的标题:a、b、c、d、e、f、g、h、i、k

数据集2的标题:a、b、c、d、e、f、g、h、i、j(分析不需要的附加列)、k

非常感谢。在


Tags: csv数据numpy标题npcombinedconcatenate设法
1条回答
网友
1楼 · 发布于 2024-05-19 05:06:52

您可以使用^{}删除多余的列,然后使用np.concatenate

headers = list('abcdefghik')
a = np.arange(len(headers)).reshape(1, -1)
#Output: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

headers_2 = list('abcdefghijk')
b = np.arange(len(headers_2)*2).reshape(2,-1)
#Output: array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10],
#       [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]])

col_to_remove = headers_2.index('j')
np.delete(b, col_to_remove, axis = 1) #note that this does not modify original array, returns a copy.
#Output: array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8, 10],
#       [11, 12, 13, 14, 15, 16, 17, 18, 19, 21]])

result = np.concatenate((a, np.delete(b, col_to_remove, axis = 1)))
#Output: array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
#       [ 0,  1,  2,  3,  4,  5,  6,  7,  8, 10],
#       [11, 12, 13, 14, 15, 16, 17, 18, 19, 21]])

相关问题 更多 >

    热门问题