2017-02-08 56 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 

你可以,如果你頭部添加到該要求。

相關問題