How to read a CSV without the first column

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)