如何将1列的数组拆分为多列(numpy数组)

我目前有一个.txt文件,该文件已作为列表加载到python中,然后作为文件的单个列放入np数组中,根据文件大小,行数为n。该文件的顶部和底部修剪了几行以进行清理。相关代码如下所示:

# Open the .txt file in the user selected folder

txtFile = glob.glob(folderView + "**/*.txt",recursive = True)
print (" The text files in the folder " + userInput + " are: " + str(txtFile))
# The quantitative output epatemp.txt file is always index position 1 in the glob list.
epaTemp = txtFile[1]
print (epaTemp)

###

quantData = open(epaTemp,'r')
print (quantData)
result = [line.split(',') for line in quantData.readlines()[24:]]
print (result)
n = 4
result = result[:-n or None]
print (result)
print (" Length of List: " + str(len(result)))
print (result[0])

### Loop through all list components and split each
print (" NUMPY STUFF! ")
list_array = np.array(result)
print (list_array)

然后像这样打印数组:比我要处理的1990年代的.txt文件更干净...

如何将1列的数组拆分为多列(numpy数组)

我的问题是,在上面列出的代码的最后几行中,将打开的文件定义为numpy数组后,无法将该单列拆分为多列。我正在尝试为所有用空格" "分隔的列创建一个列,但是这样做很麻烦。可能还需要注意的是,当我使用numpy.shape(list_array)时,它不会返回任何值,就好像数组未正确解释一样。总体目标是针对文本文件中每个由" "分割的值,将此1col x n 行数组转换为7col x n 行。如果有人可以帮助我解决这个问题,我将不胜感激。

ykxsky 回答:如何将1列的数组拆分为多列(numpy数组)

全部固定。我确实错误地分割了txt文件,使用line.split()解决了该问题。 pandas也很有必要,因为这不是np的最佳应用。感谢大家的反馈。

本文链接:https://www.f2er.com/3061909.html

大家都在问