2017-02-08 88 views
-1

我的代码:所有输入数组必须具有相同的维数蟒

GL_A = [2,3,4,5,6,7,8] 
DB_c = [88] 

writer.writerow(numpy.append(GL_A[i],DB_c[j], index)) 

的错误:

Traceback (most recent call last):  
    File "<ipython-input-61-16267abc0150>", line 1, in <module> 
    runfile('C:/Users/cp1/PythonScript/linearregression.py', wdir='C:/Users/cp1/PythonScript') 
    File "C:\Users\cp1\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile 
    execfile(filename, namespace) 
    File "C:\Users\cp1\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile 
    exec(compile(f.read(), filename, 'exec'), namespace) 
    File "C:/Users/cp1/PythonScript/linearregression.py", line 59, in <module> 
    writer.writerow(numpy.append(GL_Account_Number[i],DB_clusters[j], index)) 
    File "C:\Users\cp1\AppData\Local\Continuum\Anaconda3\lib\site-packages\numpy\lib\function_base.py", line 4586, in append 
    return concatenate((arr, values), axis=axis)  
ValueError: all the input arrays must have same number of dimensions 

,我想有输出是COLUMN1所有GL_A的元素和重复88的列2(DB_c)

有什么建议吗?

+0

这是否解决了您的问题? – Chuck

回答

1

The output that I would like to have is column1 with all element of GL_A and a column2 with repetition of 88 (DB_c)

1)创建一个新的数组,其是相同的长度GL_A其中载DB_c单值重复:

GL_A = [2,3,4,5,6,7,8] 
DB_c = [88] 
DB_cn = DB_c * len(GL_A) 

>>> 
DB_cn = [88, 88, 88, 88, 88, 88, 88] 

然后您将具有相同的长度的两个阵列中能够保存作为2个独立的列。

共有:

2)使用zip得到你想要的输出加入两个数组

GL_A = [2,3,4,5,6,7,8] 
DB_c = [88] 
DB_cn = DB_c * len(GL_A) 

import csv 

with open('some.csv', 'wb') as csvfile: 
    writer = csv.writer(csvfile) 
    writer.writerows(zip(GL_A,DB_cn)) 

和您的文件:

>>> some.csv Output: 

2 88 
3 88 
4 88 
5 88 
6 88 
7 88 
8 88 

你可以,如果你头部添加到该要求。

相关问题