You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用np.loadtxt导入文件时遇ValueError无效列索引错误求助

解决np.loadtxt导入时的ValueError错误

错误原因

报错"ValueError: invalid column index 2 at row 1 with 2 columns"的核心问题是:numpy的列索引从0开始计数,你代码中usecols=(1,2)试图读取第2列和第3列,但实际跳过前2行后的数据每行只有2列(索引为0和1),索引2超出了有效范围。

修复方案

根据你提供的文件内容,修改usecols参数为正确的列索引即可:

from google.colab import drive
drive.mount('/content/drive')

%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt


evo0_8f = '/content/drive/MyDrive/HR-Diagram/evo_0.8.txt'
# 调整usecols为(0,1),对应文件中的两列数据
evo0_8 = np.loadtxt(evo0_8f, skiprows=2, usecols=(0,1))

额外验证步骤

如果不确定数据列数,可以先不指定usecols加载全部数据,查看数据结构:

# 临时加载数据查看行列数
temp_data = np.loadtxt(evo0_8f, skiprows=2)
print(temp_data.shape)  # 输出格式为(行数, 列数),确认列数后再调整usecols参数

内容的提问来源于stack exchange,提问作者keller

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 02:22:08