Numpy - Reading a csv file with string header and row names

Viewed 270

I'm starting to learn using Numpy and I have this CSV table:

,August,September,October,November,December,January
Johny,84,81.3,82.8,80.1,77.4,75.2
Dave,79.6,75.2,75,74.3,72.8,71.4
Ali,67.5,66.5,65.3,65.9,65.6,64
Bob,110.7,108.2,104.1,101,98.3,95.5

I have to get the raw numbers in an array and also the months and names as array of strings in a list.

I managed the first one but struggle finding the right way for the other two.

My code:

def load_training_data(filename):
    data = np.genfromtxt(filename, delimiter=',',skip_header=1)
    data = data[:, 1:]
    column_names = np.asarray(np.genfromtxt(open(filename),delimiter=",",dtype=None)[:1])
    column_names = column_names.dtype.names
    row_names = np.array(filename)
    return data,column_names,row_names
1 Answers

You can use numpy.genfromtxt(...,skip_header=0) to retrieve data from the csv file. However, the strings will be formatted to type numpy.bytes_, so you need to convert that to string using decode('UTF-8') function.

def load_training_data(filename):
    data = np.genfromtxt(filename, delimiter=",", skip_header=0, dtype=None)
    column_names = [name.decode('UTF-8') for name in data[0,:]][1:]
    person_names = [name.decode('UTF-8') for name in data[:,0]][1:]
    return data[1:,1:].astype(float), column_names, person_names
Related