[Tutor] Change datatype for specific columns in an 2D array & computing the mean

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Jan 28 05:28:17 EST 2016


On 27 January 2016 at 23:00, Ek Esawi <esawiek at gmail.com> wrote:
> Ops..here is the text file.; previously i copied and pasted from either
> Word or Excel.
>
>
> AA,BB,CC,DD,EE
> 1,A1,B1,11.2,11/20/2011
> 2,A2,B2,2.5,10/21/2011
> 3,A3,B3,13.67,9/21/2011
> 4,A4,B4,14.2,8/22/2011
> 5,A5,B5,20,7/23/2011

Finally! That's what I expect to see in a csv file. Do the first,
second and third columns just count up like that? I thought that you
were expecting some rows to have the same values in the second and
third columns.

I'm not sure that it's really worth using numpy for this. I'd just use
the csv module:

import csv

with open('test.csv') as csvfile:
    reader = csv.reader(csvfile)
    next(reader, None) # Ignore first line of file
    for line in reader:
        index, col2, col3, col4, date = line
        col4 = float(col4)  # Convert 4th column value to float
        print([col2, col3, col4])

Running this I get:

$ python3 test.py
['A1', 'B1', 11.2]
['A2', 'B2', 2.5]
['A3', 'B3', 13.67]
['A4', 'B4', 14.2]
['A5', 'B5', 20.0]

--
Oscar


More information about the Tutor mailing list