Instagram
youtube
Facebook
Twitter

Creating and Importing Array from CSV

  • Most of the time, you won't be creating data, instead, you will import or add an array of other resources.

  • Here, we are going to use a CSV file for importing the data, CSV files are (Comma-Separated Values) and by using the function np.genfromtxt(), this function has parameters one is the name of the file from which you are importing the data and the other is delimiter which is used to separate values in the file, usually delimiters is a comma ',' but sometimes there will be other delimiters as well.

Let's have a coding example of this as below:

Suppose we have a file named 'values.csv' which has the following values

2, 3, 5, 76, 21, 4, 88, 12

Now we can import this CSV file data using the code below:

import numpy as np

arr1 = np.array([92, 4, 88, 11, 87]) #Creating an Array

arr2 = np.genfromtxt('values.csv', delimiter=',') #Importing an Array 

print(arr2) #prints [2, 3, 5, 76, 21, 4, 88, 12]