Open In App

How to Read CSV Files with NumPy?

Last Updated : 30 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to read CSV files with Numpy in Python. Reading CSV files using Python NumPy library helps in loading large amount of data quicker. Due to performance reason, NumPy is preferred while reading huge amount of data from CSV files.

Dataset in use:

Read CSV Files with NumPy

Read CSV Files with NumPy

Read CSV Files using built-in Python open() function

Here we are not using any third-party library in Python. We are just opening the file and reading it line by line using built-in Python open() function.

Python3




# using open function
 
file = open("data.csv")
 
for i in file:
    print(i)


Output:

Read CSV Files with NumPy

Reading CSV files using open() function

Read CSV files Using NumPy loadtxt() method

To import data from a text file, we will use the NumPy loadtxt() method. To use this function, we need to make sure that the count of entries in each line of the text document should be equal. In Python, numpy.load() is used to load data from a text file, with the goal of being a quick read for basic text files.

Syntax: numpy.loadtxt(‘data.csv’)

Parameters:

  • fname: The file name to load data from.
  • delimiter (optional): Delimiter to consider while creating array of values from text, default is whitespace.
  • encoding (optional): Encoding used to decode the inputfile.
  • dtype (optional): Data type of the resulting array

Return: returns NumPy array

Example: Loading csv using numpy loadtxt() method

Python3




import numpy as np
 
# using loadtxt()
arr = np.loadtxt("sample_data.csv",
                 delimiter=",", dtype=str)
display(arr)


Output:

Read CSV Files with NumPy

Read CSV files Using NumPy genfromtxt() method

The genfromtxt() method is used to import the data from a text document. We can specify how to handle the missing values if there are any.

Syntax: numpy.genfromtxt(‘data.csv’)

Parameters:

  • fname: The file to read from
  • delimiter (optional): Delimiter to consider while creating array of values from text, default is any consecutive white spaces act as a delimiter.
  • missing_values (optional): The set of strings to use incase of a missing value.
  • dtype (optional): Data type of the resulting array

Return: returns NumPy array

Example: Loading CSV data using numpy genfromtxt() method

Python3




import numpy as np
 
# using genfromtxt()
arr = np.genfromtxt("sample_data.csv",
                    delimiter=",", dtype=str)
display(arr)


Output:

Read CSV Files with NumPy

Read CSV files Using Pandas read_csv() function

Here we are just creating the dataframe, with the help of values function we get the array of values in the file.

Python3




#importing required library
from pandas import read_csv
 
d = read_csv('data.csv')
 
df = d.values
print(df)


Output:

Read CSV Files with NumPy

Read CSV Files with NumPy

Read CSV files Using built-in Python csv module

csv.reader() function reads each line of the CSV file. We read data line by line and then convert each line to a list of items.

Syntax: csv.reader(x)

Parameters:

  • x : TextIOWrapper of the CSV file.

Example: Loading CSV data using csv reader

Python3




import numpy as np
 
# Importing csv module
import csv
 
 
with open("sample_data.csv", 'r') as x:
    sample_data = list(csv.reader(x, delimiter=","))
 
sample_data = np.array(sample_data)
display(sample_data)


Output:

Read CSV Files with NumPy

Read CSV Files with NumPy



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads