For example, I have csv file contains ten columns. Is it possible to read it into two variables -- first one will contain 1-2..9 columns and second will contain last column by means of loadtxt or genfromtxt?
3 Answers
a = numpy.loadtxt('blah.csv', delimiter=',') # loads it all into one big array
a1 = a[:, :-1] # a view of all but the last column
a2 = a[:, [-1]] # a copy of just the last column
(Or, if you want your last-column variable to be one-dimensional, you can get a view of it using a[:,-1] instead of saying a[:, [-1]].)
4 Comments
a[:, [-1]] will be a view, but rather, a copy.a[:, -1][:, np.newaxis] be? a view or copy?numpy.loadtext('filename.csv', usecols(0,1,2,...,n-2))
numpy.loadtext('filename.csv', usecols = n-1)
Note: The index of your columns should be decremented by 1, as numpy would start at 0 when loading.
From the Documentation https://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html:
usecols : int or sequence, optional Which columns to read, with 0 being the first. For example, usecols = (1,4,5) will extract the 2nd, 5th and 6th columns. The default, None, >results in all columns being read."
Comments
In case you don't really need numpy. You could use pandas (http://pandas.pydata.org/), or csv (https://docs.python.org/2/library/csv.html) libraries.
a(very easy withloadtxt) and then look at two different slices,a1 = a[:, :-1]anda2 = a[:, [-1]]numpyquestion, which already has functions to load csvs directly intonumpydata structures. Usingcsvin this case is an unnecessary middleman.