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

Ek Esawi esawiek at gmail.com
Mon Jan 25 22:07:10 EST 2016


Thank you all for your patience and dedication to this noble effort-the
list. I actually completed my project using Excel and python; unfortunately
my python code is not a python-like at all b/c I used way too many loops
and if statements. Plus I really want to learn python b/c I am a decent
programmer in other languages.


Here is a copy of my code and the csv file. It’s similar to the one I
posted earlier. For now I am not even concerned with mean or other
computations. All I want is to be able read a csv file which has string,
float, integer, date, and etc. datatypes. These are the majority of file
types I work with. I want to transfer it to a python array with proper
datatype. For example on my file, I want the 1st column integer, the 4th
float, the 2nd and 3rd string and the 5th date. I spent a few hours looking
online and in python documentation and did not find anything simple for me
to use.


I tried the function posted here but it’s too complicated for me-you can
see what I know about python. If there is no simpler way to do this then
that’s OK. I will keep on reading and see what happens.

I even tried to do some of it manually but did not work, as you see in my
code. Thank you all for your continuous help and dedication to this great
site----EKE


++ Code++

import numpy as np

import csv



DataMat=[]

np.set_printoptions(precision=2)



with open('c:/Users/EK Esawi/My Documents/Python Scripts/TestFile1.csv') as
Test1:

    reader = csv.reader(Test1, delimiter=',')

#    next(Test1)

    for row in reader:

        DataMat.append(row)



DataMat=np.array(DataMat)



col1=np.array(DataMat[1::,0], dtype=int)

col2=np.array(DataMat[1::,1])

col3=np.array(DataMat[1::,1])

col4=np.array(DataMat[1::,3], dtype=float)

col5=np.array(DataMat[1::,4])



++ csv file++

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


More information about the Tutor mailing list