How to read a CSV without the first column

You could use pandas and read it as a DataFrame object. If you know the column that you do not want, just add a .drop to the loading line:

a = pandas.read_csv("Data/sim.csv",sep=",")
a = a.drop(a.columns[0], axis=1)

The first row will be read as a header, but you can add a skiprows=1 in the read_csv parameter. Pandas DataFrames are numpy arrays, so, converting columns or matrices to numpy arrays is pretty straightforward.


jmilloy and Deninhos's answers are both good. If OP specifically wants to read in an NumPy array (as opposed to pandas dataframe), another simplistic alternative is to delete the index column after reading it in. This works when you know the index column is always the first, but number of features (columns) are flexible.

data = np.loadtxt("Data/sim.csv", delimiter=',', skiprows=1)
data = np.delete(data, 0, axis = 1)

You can specify a converter for any column.

converters = {0: lambda s: float(s.strip('"')}
data = np.loadtxt("Data/sim.csv", delimiter=',', skiprows=1, converters=converters)

Or, you can specify which columns to use, something like:

data = np.loadtxt("Data/sim.csv", delimiter=',', skiprows=1, usecols=range(1,15))

http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html


One way you can skip the first column, without knowing the number of columns, is to read the number of columns from the csv manually. It's easy enough, although you may need to tweak this on occasion to account for formatting inconsistencies*.

with open("Data/sim.csv") as f:
    ncols = len(f.readline().split(','))

data = np.loadtxt("Data/sim.csv", delimiter=',', skiprows=1, usecols=range(1,ncols+1))

*If there are blank lines at the top, you'll need to skip them. If there may be commas in the field headers, you should count columns using the first data line instead. So, if you have specific problems, I can add some details to make the code more robust.

Tags:

Python

Csv

Numpy