所有输入数组必须具有相同数量的维度python

时间:2017-02-08 09:50:42

标签: python

我的代码:

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(DB_c)的column2

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

  

我想要的输出是column1,其中包含GL_A的所有元素和一个重复为88(DB_c)的column2

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

如果需要,您可以为此添加标题。