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:
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:
Contact Us