Python 2.7-将1D数组和2D数组保存在同一csv中

时间:2019-05-08 14:30:48

标签: python arrays csv numpy

我有一个sgape(4)的一维数组,如下所示:

['rsmin_025_lai_chtessel' 
 'rsmin_050_lai_chtessel'
 'rsmin_075_lai_chtessel' 
 'rsmin_100_lai_chtessel']

和形状为(4,12)的2D数组,如下所示:

[[ 8.  9. 20. 20. 19. 20. 20. 20. 20. 20. 20. 20.]
 [ 6.  8. 19. 17.  7. 19. 19. 19. 19. 19. 19. 19.]
 [ 4.  6. 18. 14.  3. 18. 15. 18. 18. 18. 18. 18.]
 [ 2.  4. 17. 11.  1. 17. 11. 14. 17. 17. 17. 17.]]

现在,我想将这2个数组保存到csv中,为此,我使用以下代码:

np.savetxt('jojo23.txt', (array1,array2))

返回以下错误:

ValueError: could not broadcast input array from shape (20,12) into shape (20)

有人知道如何合并这两个数组以便获得以下csv输出:

rsmin_025_lai_chtessel,8,9,20,20,19,20,20,20,20,20,20,20
rsmin_050_lai_chtessel,6,8,19,17,7,19,19,19,19,19,19,19
rsmin_075_lai_chtessel,4,6,18,14,3,18,15,18,18,18,18,18
rsmin_100_lai_chtessel,2,4,17,11,1,17,11,14,17,17,17,17

1 个答案:

答案 0 :(得分:1)

您可能希望对此类数据使用JSON格式。

否则,请尝试:

np.savetxt('jojo23.txt', np.c_[array1,array2])

np.c_[...]是执行列级连接的快速方法。