简体   繁体   English

在二维numpy数组中填充单元格时出现错误消息

[英]error message when populating cell in 2d numpy array

I am trying to populate data from some csv files into a numpy array with the following code: 我正在尝试使用以下代码将一些csv文件中的数据填充到numpy数组中:

PreExArray=zeros([len(TestIDs),numColumns],float)

for row in reader:
    if row[1] =='PreEx10SecondsBEFORE':
        PreExArray[(j-1),0]=[row[2]]

However, the last line of code above throws the following error: 但是,上面的代码的最后一行抛出以下错误:

ValueError: setting an array element with a sequence.

So I printed out the contents of row[2] as follows: 所以我打印出row [2]的内容,如下所示:

print 'row[2] is:  ',row[2]

This produced: 这产生了:

row[2] is: 0.780083333333 行[2]是:0.780083333333

So the contents of row[2] are not a sequence as the error message indicates. 因此,row [2]的内容不是错误消息所指示的序列。 Instead, the contents are a number. 而是内容是一个数字。

Therefore, I used to following code to put the contents of row[2] into a variable, then populate PreExArray[(j-1),0] with the contents of that variable, and then print out the contents of that variable: 因此,我习惯于遵循以下代码将row [2]的内容放入一个变量,然后用该变量的内容填充PreExArray [(j-1),0],然后打印出该变量的内容:

jones = row[2]
PreExArray[(j-1),0]=jones
print 'PreExArray[(j-1),0] is:  ',PreExArray[(j-1),0]

The result is: 结果是:

PreExArray[(j-1),0] is: 0.780083333333 PreExArray [(j-1),0]是:0.780083333333

So, putting row[2] into a variable solves the problem. 因此,将row [2]放入变量即可解决该问题。 But this is really sloppy code if I have to put it into a variable every time. 但这确实是草率的代码,如果我每次必须将其放入变量中。

Can anyone show me how to fix the code so that it does not throw an error when I type something a lot simpler, like PreExArray[(j-1),0]=[row[2]] ? 谁能告诉我如何修复代码,以便在我键入更简单的内容(例如PreExArray [(j-1),0] = [row [2]])时不会出现错误?

=========================================================================================== ================================================== ========================================

OK. 好。 I re-wrote the code, and now it is throwing a new error. 我重新编写了代码,现在它引发了一个新错误。 The new code is as follows: 新代码如下:

PreExArray=zeros([len(TestIDs),numColumns],float) 

for row in reader: 
    if row[1] =='PreEx10SecondsBEFORE': 
        PreExArray[(j-1),0]=row[1]
        PreExArray[(j-1),1]=row[2]

This revised code now throws the following error message: 现在,此修订的代码将引发以下错误消息:

PreExArray[(j-1),0]=row[1]
ValueError: setting an array element with a sequence.

However, when I comment out PreExArray[(j-1),0]=row[1] as follows (#PreExArray[(j-1),0]=row[1]), the subsequent lines run without throwing an error. 但是,当我按如下方式注释掉PreExArray [[j-1),0] = row [1](#PreExArray [[j-1),0] = row [1])时,后续行运行时不会引发错误。

Can anyone tell me how to edit this so that it does not continue to throw this error? 谁能告诉我如何编辑它,以便它不会继续引发此错误?

You should just have: 您应该只有:

PreExArray[(j-1),0]=row[2]

That is, the right hand side should NOT be put into a length-1 list. 也就是说,不应将右侧放在长度为1的列表中。

It looks like your row variables are from a spreadsheet, and the first index values are row labels (strings). 看来您的行变量来自电子表格,并且第一个索引值是行标签(字符串)。 As has been pointed out, you cannot store this data in a numpy array of datatype 'float'. 如前所述,您不能将此数据存储在数据类型为“ float”的numpy数组中。

The error message you get arises from the fact that to numpy, a string looks like a sequence (probably because it supports a len attribute.) 您收到的错误消息来自以下事实:对于numpy,字符串看起来像一个序列(可能是因为它支持len属性)。

If you want to save the row labels in order to reconstruct a spreadsheet importable file, with the new values you calculate, you will have to store them in a separate list. 如果要保存行标签以重建电子表格可导入文件,请使用计算出的新值将它们存储在单独的列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM