2

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 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Yes everywhere I look online pandas is more recommended in this situation. Unfortunately I need to submit it using numpy
I tried to find a numpy solution and found one. I edited my answer above, you can test it for yourself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.