如何将NUMPY数组导出到CSV或EXCEL文件

时间:2018-10-19 06:29:25

标签: python arrays excel csv numpy

这是我第一次尝试使用Python进行编码,因此我学习了如何制作一个numpy数组,以及如何使用np.savetxt将其导出为CSV文件。但是,当我在excel中打开CSV文件时,矩阵的列似乎合并为一个,因此无法对其进行分析。我想知道如何解决此问题。我不知道numpy是否适合进行此分析。因此,如果您还有其他建议,请附上。

在这里,我创建了一个尺寸为a1,b1的空数组。

# Create an empty array with dim = (a1: num of months, b1:num of stations)

aa = np.empty((a1, b1))
aa[:] = np.nan

在这里,我用for循环逐行填充了空数组:

for i in range(1, a1):

    S_Obs = Sta_M.iloc [i-1, 2]
    R_Val = Rad_M.iloc [i, 2:]


    addadjuster = adjust.AdjustAdd(coords, coords, nnear_raws = 5)
    addadjusted = addadjuster(S_Obs, R_Val)

    aa[i,:] = addadjusted 

最后,当我逐行显示数组时,它看起来像这样:

aa[111, :]

array([   nan,    nan,    nan, 16.296, 24.888,    nan,    nan,    nan,
          nan,    nan,    nan,    nan,    nan,    nan, 23.496,  1.704,
       52.32 ,    nan, 25.368,    nan,    nan,    nan,    nan,    nan,
          nan,    nan,    nan, 21.264, 19.584, 22.272,  0.144, 10.008,
        1.68 ,  0.   ,    nan,    nan,    nan,    nan,    nan,  0.   ,
        0.   , 30.696,    nan,    nan, 24.888,    nan,    nan,  3.648,
       14.832,  7.944,    nan,    nan,    nan,    nan,    nan,    nan,
          nan])

我想以一种可以对其进行一些简单分析的方式保存该数组。它可以是EXCEL或CSV。我使用了这段代码,但是没有正确显示列。

np.savetxt("AAtest.csv", aa, delimiter="/")

3 个答案:

答案 0 :(得分:6)

您可以使用Purchase来解决问题enter link description here

假设class Stock(models.Model): product_name = models.CharField(max_length=100,blank=False) quantity = models.IntegerField() price = models.FloatField() status = models.CharField(max_length=10, default='AVAILABLE') def delete(self): Sale.objects.create(product_name=self.product_name, quantity=self.quantity, price=self.price, status=self.status) super().delete() class Purchase(models.Model): product_name = models.CharField(max_length=100,blank=False) quantity = models.IntegerField() price = models.FloatField() status = models.CharField(max_length=10,default='Purchased') def save(self): if not self.pk: Stock.objects.create(product_name=self.product_name, quantity=self.quantity, price=self.price, status=self.status) super().save() 是您的numpy数组。您可以这样做:

Pandas

答案 1 :(得分:3)

In [155]: arr = np.zeros((4,5))
In [156]: arr[:] = np.nan
In [158]: arr[[0,0,1,2,2,3],[0,2,1,3,4,3]]=1.23
In [159]: arr
Out[159]: 
array([[1.23,  nan, 1.23,  nan,  nan],
       [ nan, 1.23,  nan,  nan,  nan],
       [ nan,  nan,  nan, 1.23, 1.23],
       [ nan,  nan,  nan, 1.23,  nan]])
In [160]: np.savetxt('test.csv',arr, delimiter=',')

这是格式正确的逗号分隔文件。但是数字是用科学记数法保存的。

In [161]: cat test.csv
1.229999999999999982e+00,nan,1.229999999999999982e+00,nan,nan
nan,1.229999999999999982e+00,nan,nan,nan
nan,nan,nan,1.229999999999999982e+00,1.229999999999999982e+00
nan,nan,nan,1.229999999999999982e+00,nan

要排成一列,我们需要指定一种格式。例如:

In [162]: np.savetxt('test.csv',arr, delimiter=',', fmt='%10f')
In [163]: cat test.csv
  1.230000,       nan,  1.230000,       nan,       nan
       nan,  1.230000,       nan,       nan,       nan
       nan,       nan,       nan,  1.230000,  1.230000
       nan,       nan,       nan,  1.230000,       nan

答案 2 :(得分:1)

如果由于某种原因无法使用pandas,请坚持使用numpy

aa.tofile('my_csv.csv', sep=',', format='%s')