[Numpy-discussion] newbie: attempt at data frame
Sven Schreiber
svetosch at gmx.net
Wed Jan 3 06:07:15 EST 2007
Sven Schreiber schrieb:
> Hi Vincent, of course it depends a little on how exactly your csv file
> looks like, but if you just have column headers and the actual data, you
> might try something like the following:
>
Ok sorry the previous thing doesn't work, I also stumbled over the
strings. Here's the next attempt, also shorter. (this time even tested ;-)
import csv
from numpy import mat
read_from = csv.reader(file(filename, 'r'), skipinitialspace = True)
stringlist = [ line for line in read_from ]
varnames = stringlist.pop(0)[1:]
datalist = [ map(float, line[1:]) for line in stringlist ]
# now the real data
data = mat(datalist, dtype = float)
I actually quite like it... python lists are very nice. This discards
the observation labels, but it's not difficult to add that, of course.
-sven
More information about the NumPy-Discussion
mailing list