[SciPy-User] calculate new values in csv using numpy

John Hunter jdh2358 at gmail.com
Mon Jul 18 11:01:21 EDT 2011


On Mon, Jul 18, 2011 at 5:17 AM, Johannes Radinger
<johradinger at googlemail.com> wrote:
> Hello,
> I try to load a csv-file (created with excel) and want to calculate a new
> value.
> Lets say my file has columns A,B and C and I want to calculate A*B-C and
> append
> it in the correct line.
> So far I managed to get the file read with:
> table = numpy.genfromtxt("/path/to/file.csv", dtype=None, delimiter=';',
> skip_header=1)
> But how do I have to proceed to get the single columns. I know that I have
> to use after that a FOR-loop to loop over the lines to do the calculation.
> so there are the two questions:
> 1) how to extract the single columns?
> 2) how to append a new columns containing the calculated value?

import matplotlib.mlab as mlab
r = mlab.csv2rec("somefile.csv")
z = r.x + r.y
r = mlab.rec_append_fields(r, ['z'], [z])
mlab.rec2csv(r, 'newfile.csv')



More information about the SciPy-User mailing list