【问题标题】:Numpy loadtxt in python - except some columspython 中的 Numpy loadtxt - 除了一些列
【发布时间】:2023-01-12 23:45:05
【问题描述】:

是否可以在 np.loadtxt 中加载除第一列或某些特定列以外的所有列?

请问 usecols = (0,) 是什么意思? 是否可以使用滑块?

我有 55 列,我想加载除一列以外的所有列。有没有比写usecols = (1, 2, 3, 4, 5, 6, 7) 并继续到 55 更好的方法?

【问题讨论】:

  • “滑块”是什么意思?

标签: numpy python-3.6


【解决方案1】:

我不认为有一种方法可以在不指定所有列的情况下排除某些列。

由于您只想排除一列,您不妨阅读整个内容,然后使用 data = data[1:] 将其切掉。

如果你真的不想这样做,你可以做usecols=range(1, 56)而不是输入所有的数字。

对于更通用的方法,您可以编写一个函数,该函数采用要排除的列数和列列表,并自动创建 usecols 参数:

def loadtxt_excludecols(exclude_cols, num_cols, *args, **kwargs):
    cols = set(range(num_cols))
    cols -= set(exclude_cols)
    cols = sorted(list(cols))
    return np.loadtxt(*args, **kwargs, usecols=cols)

data = loadtxt_excluldecols([1, 10, 30], 50, 'filename.dat', ...other loadtxt args)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-26
    • 2014-05-21
    • 2013-06-13
    • 2022-12-13
    • 1970-01-01
    • 2023-03-19
    • 2017-12-21
    • 2015-09-29
    相关资源
    最近更新 更多