Skip to Content
Introduction to NumPy
Creating an Array from a CSV

Typically, you won’t be entering data directly into an array. Instead, you’ll be importing the data from somewhere else.

We’re able to transform CSV (comma-separated values) files into arrays using the np.genfromtxt() function:

Consider the following CSV, sample.csv,


We can import this into a NumPy array using the following code:

csv_array = np.genfromtxt('sample.csv', delimiter=',')

Note that in this case, our file sample.csv has values separated by commas, so we use delimiter=',', but sometimes you’ll find files with other delimiters, the most common being tabs or colons.

Once imported, this CSV will create the array

>>> csv_array array([34, 9, 12, 11, 7])



You found that inputting the student’s scores into an array by hand was too time-consuming. Import the student’s scores on the second test from the CSV test_2.csv into an array.

Save the array with the name test_2.

Folder Icon

Sign up to start coding

Already have an account?