How to read CSV to NumPy Array

In this post, we are going to learn how to read CSV to NumPy Array using loadtxt(),genfromtxt(), using csv module, using pandas with examples.

1. Numpy loadtxt() read csv with header


We will need to import numpy to our program to be able to use the functions available in numpy.

  • To load data using the function loadtxt() from numpy.We have passed the name of the file, but you will need to make sure your file name is with the correct path location to the file.
  • We have passed the file name “data.csv” with parameters
    • delimiter =’,’ to specify the delimiter to separate contents of file
    • dtype : To specify the type of datatype.
    • Encoding : This is used to decode the input file.
import numpy as np
data = np.loadtxt("data.csv",delimiter=',',dtype=str,encoding="utf-8-sig")
print(data)

Output

[['Name' 'Marks' 'Address' 'Subject']
 ['Tony' '98' 'Chicago' 'Math']
 ['Jack' '100' 'US' 'English']
 ['John' '100' 'India' 'Music']
 ['Rim' '100' 'Canda' 'Chemistry']]

2. Numpy read csv with column name


If our data have some of the values missing in any column or row then we can fill the missing values by using the genfromtxt() function. This function loads our data and also fills the missing values with ‘nan’ which means not a number.

We have passed the file name “data.csv” with parameters

  • delimiter =’,’ to specify the delimiter to separate contents of file
  • dtype : To specify the type of datatype.
  • Encoding : This is used to decode the input file.
import numpy as np
data = np.genfromtxt('data.csv', delimiter = ',',dtype=str,encoding='UTF-8')
print(data)

Output

[['Name' 'Marks' 'Address' 'Subject']
 ['Tony' '98' 'Chicago' 'Math']
 ['Jack' '100' 'US' 'English']
 ['John' '100' 'India' 'Music']
 ['Rim' '100' 'Canda' 'Chemistry']]

3. CSV Module to read CSV to NumPy array


The CSV module implements a class to write and read tabular data in a CSV file. The CSV.writer() method is used to write CSV file.The CSV.reader() method is used to read a csv file.In this example, we are reading all contents of the file, Finally using the np. array() to convert file contents in a numpy array.

  • Import the NumPy module using “import numpy as np”
  • Import the CSV module using “import csv”.
  • The csv.reader() method to read file contents and np.array() to convert in numy array.
import csv
import numpy as np
 
with open('data.csv', 'r') as file:
    file_content = list(csv.reader(file, delimiter=","))
 
data = np.array(file_content)
print(file_content)

Output

[['Name', 'Marks', 'Address', 'Subject'], ['Tony', '98', 'Chicago', 'Math'], ['Jack', '100', 'US', 'English'], ['John', '100', 'India', 'Music'], ['Rim', '100', 'Canada', 'Chemistry']]

4. Pandas Module read CSV to NumPy Array


The read_CSV() method reads the file contents at the given path in the dataframe. It uses a comma as a defualt separator or delimiter. The comma, tab, or even a custom delimiter or regular expression can be used as a delimiter.

import pandas
content = pandas.read_csv('data.csv')
print(content)

Output

   Name  Marks  Address    Subject
0  Tony     98  Chicago       Math
1  Jack    100       US    English
2  John    100    India      Music
3   Rim    100    Canada  Chemistry

5. Read CSV without using any library


In the python program example, we are using the python built-in method open() to read a csv file in a numpy array. We have opened the file in reading mode (“r”) in the open() method. The for loop to iterate through rows of files.

  • The csv is comma-separated(,) file.So split the contents using comma(,).
  • The list.append() to append contents of file into a list
  • FInally printing the result using print method.
with open('data.csv', 'r') as csvfile:
    contents = []
    for line in csvfile:
            words = line.split(',')
            contents.append((words[0], words[1:]))
    print(contents)

Output

[('Name', ['Marks', 'Address', 'Subject\n']), ('Tony', ['98', 'Chicago', 'Math\n']), ('Jack', ['100', 'US', 'English\n']), ('John', ['100', 'India', 'Music\n']), ('Rim', ['100', 'Canda', 'Chemistry\n'])]


Summary

In this post, we have learned How to read CSV to NumPy Array by using the different built-in method