How to put data from a CSV file in a matrix in python using numpy ?

Published: May 17, 2021

Tags: Python; Numpy; CSV;

DMCA.com Protection Status

In python, there are multiple solutions to read a csv (comma separated values) file:

Important note: use only numpy to read a csv file if it contains only one type of data (for example a csv file with numbers only). If the csv file contains columns with different types like strings and integers, a better solution is to pandas then, see how to read a csv file using pandas in python

Read a csv file using numpy loadtxt

Let's try to read the following github csv file:

https://raw.githubusercontent.com/edbullen/Hypothesis/master/ages.csv

to do that a solution is to use loadtxt:

ages = np.loadtxt('https://raw.githubusercontent.com/edbullen/Hypothesis/master/ages.csv', skiprows=0, delimiter=',')

returns here

array([32., 34., 29., 29., 22., 39., 38., 37., 38., 36., 30., 26., 22.,
   22.])

Notes that:

type(ages)

returns

numpy.ndarray

and

ages.dtype

returns

dtype('float64')

Read a csv file using numpy genfromtxt

Another solution is to use genfromtxt

ages = np.genfromtxt('https://raw.githubusercontent.com/edbullen/Hypothesis/master/ages.csv', delimiter=',')

References