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:
How to Read CSV Files with NumPy?
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: