Python 获取genfromtxt/loadtxt以忽略被忽略列/行中的数据类型
我有一个包含整数数据的文件,其中前几行/列用于名称 我希望能够使用Python 获取genfromtxt/loadtxt以忽略被忽略列/行中的数据类型,python,numpy,Python,Numpy,我有一个包含整数数据的文件,其中前几行/列用于名称 我希望能够使用genfromtxt或loadtxt并且仍然能够让numpy将其作为一个同质数组来读取。为此,我使用了选项skiprows和usecols,但没有帮助。 在下面的(工作)示例中,我希望print(test\u array.shape)给出(3,3)和print(test.array)给出 [[0 0 0] [0 1 0] [1 0 0]] 在尝试加载文件之前,是否有任何方法可以在不修剪第一行/第一列的情况下实现我想要的结果?
genfromtxt
或loadtxt
并且仍然能够让numpy将其作为一个同质数组来读取。为此,我使用了选项skiprows
和usecols
,但没有帮助。
在下面的(工作)示例中,我希望print(test\u array.shape)
给出(3,3)和print(test.array)
给出
[[0 0 0]
[0 1 0]
[1 0 0]]
在尝试加载文件之前,是否有任何方法可以在不修剪第一行/第一列的情况下实现我想要的结果?请注意,我要加载的实际文件是B-I-G(~6 gigs),因此任何解决方案都不应该过于计算密集
from __future__ import print_function
from StringIO import StringIO #use io.StringIO with py3
import numpy as np
example_file = StringIO("FID 1 2 3\n11464_ATCACG 0 0 0\n11465_CGATGT 0 1 0\n11466_TTAGGC 1 0 0")
test_array = np.loadtxt(example_file, skiprows=1, usecols=(1,), dtype=int)
print(test_array.shape) #(3,)
print(test_array) #[0 0 1]
您可以使用
np.genfromtxt
中的usecols
和skip_header
标志。那么它的工作原理是:
test_array = np.genfromtxt(example_file, skip_header=1, usecols=(1,2,3))
>>> print(test_array)
[[ 0. 0. 0.]
[ 0. 1. 0.]
[ 1. 0. 0.]]
您可以使用
np.genfromtxt
中的usecols
和skip_header
标志。那么它的工作原理是:
test_array = np.genfromtxt(example_file, skip_header=1, usecols=(1,2,3))
>>> print(test_array)
[[ 0. 0. 0.]
[ 0. 1. 0.]
[ 1. 0. 0.]]