gpt4 book ai didi

Python索引错误: too many indices for array when trying to append two csv files

转载 作者:行者123 更新时间:2023-12-01 03:39:03 24 4
gpt4 key购买 nike

每当我尝试将两个 csv 文件附加在一起时,我都会收到此错误。

log1 = np.genfromtxt('log40a.csv', dtype = float, delimiter=',', skip_header =1)
log2 = np.genfromtxt('log40b.csv', dtype = float, delimiter=',', skip_header= 1)
data = np.append(log1, log2)

这是我遇到的错误。

mSec = data[:,0]

如果我不附加两个 csv 并且只绘制 log1 文件,它工作正常,但是当我由于某种原因尝试附加它们时,出现错误:

  File "<ipython-input-5-6155c8de61ad>", line 1, in <module>
runfile('C:/Users/myname/.spyder2-py3/setdataexp.py', wdir='C:/Users/Myname/.spyder2-py3')

File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
execfile(filename, namespace)

File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Myname/.spyder2-py3/setdataexp.py", line 11, in <module>
mSec = data[:,0]

IndexError: too many indices for array

编辑:我忘记添加 csv 文件的示例

mSec,speed,heading,gspeed,....
20,3.5,3,4.5
21,3,4,5
22,3.5,6,5.5

最佳答案

给出 python 文档并且您正在使用 numpay,您需要检查此方法以融合现有数组numpy.concatenateEx 取自文档:

>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]])
>>> np.concatenate((a, b), axis=0)
array([[1, 2],
[3, 4],
[5, 6]])

Documetation of numpay

并且您还有更多方法来融合数组:

关于Python索引错误: too many indices for array when trying to append two csv files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39964775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com