In this article, we will learn how to Read CSV file into a NumPy Array in Python.

**Table Of Contents**

## What is CSV File?

A CSV file is a comma-separated values file. The csv file format allows data to be saved in a tabular format. The CSV file is just a plain text file in which data is separated by commas.

Suppose we have a csv file: **data.csv** and its contents are as follow,

1,2,3,4,5 6,7,8,9,0 2,3,4,5,6 4,5,6,7,7

Now we want to load this CSV file into a NumPy Array.

There are multiple ways to Read CSV file into a NumPy Array in Python. Lets discuss all the methods one by one with proper approach and a working code example

## Read CSV File into a NumPy Array using loadtxt()

The numpy module has a loadtxt() function and it is used to load data from a text file. Each row in the text file must have same number of values.

### Frequently Asked:

- Check if all elements in a NumPy array are unique
- Python : Find unique values in a numpy array with frequency & indices | numpy.unique()
- Create Numpy Array of different shapes & initialize with identical values using numpy.full() in Python
- Convert 2D NumPy array to list of lists in python

**Syntax of loadtxt() function**

numpy.loadtxt(fname, delimiter, skiprows)

- Parameters:
- fname = Name or path of the file to be loaded.
- delimiter = The string used to separate values, By default the delimiter is whitespace.
- skiprows = The Number of rows to be skipped.

- Returns:
- Returns an array.

**Approach:**

- Import numpy library.
- Pass the path of the csv file and delimiter as comma (,) to loadtxt() method.
- Print the Array returned by the loadtxt() method.

**Source Code**

import numpy as np # Reading csv file into numpy array arr = np.loadtxt("data.csv", delimiter=",") # printing the array print(arr)

**Output:**

[[1. 2. 3. 4. 5.] [6. 7. 8. 9. 0.] [2. 3. 4. 5. 6.] [4. 5. 6. 7. 7.]]

## Read CSV File into a NumPy Array using genfromtxt()

The numpy module have genfromtxt() function, and it is used to Load data from a text file. The genfromtxt() function can handle rows with missing values as specified.

**Syntax of genfromtxt() function**

numpy.genfromtxt(fname, delimiter)

- Parameters:
- fname = Name or path of the file to be loaded.
- delimiter = The string used to separate values, By default the delimiter is whitespace.

- Returns:
- Returns an array.

**Approach:**

- Import numpy library.
- Pass the path of the csv file and delimiter as comma (,) to genfromtxt() method.
- Print the Array returned by the genfromtxt() method.

**Source Code**

import numpy as np # Reading csv file into numpy array arr = np.genfromtxt("data.csv",delimiter=",") # printing the array print(arr)

**Output:**

[[1. 2. 3. 4. 5.] [6. 7. 8. 9. 0.] [2. 3. 4. 5. 6.] [4. 5. 6. 7. 7.]]

## Read CSV File into a NumPy Array using read_csv()

The pandas module has a read_csv() method, and it is used to Read a comma-separated values (csv) file into DataFrame, By using the values property of the dataframe we can get the numpy array.

**Syntax of read_csv() function**

numpy.read_csv(file_path, sep, header)

- Parameters:
- file_path = Name or path of the csv file to be loaded.
- sep = The string used to separate values i.e, delimiter , By default the delimiter is comma (,).
- header = The names of the columns.

- Returns:
- Returns an DataFrame.

**Approach:**

- Import pandas and numpy library.
- Pass the path of the csv file and header as None to read_csv() method.
- Now use the
**values**property of DataFrame to get numpy array from the dataframe. - Print the numpy array.

**Source Code**

import numpy as np import pandas as pd # Reading csv file into numpy array arr = pd.read_csv('data.csv', header=None).values # printing the array print(arr)

**Output:**

[[1 2 3 4 5] [6 7 8 9 0] [2 3 4 5 6] [4 5 6 7 7]]

## Read CSV File into a NumPy Array using file handling and fromstring()

Python supports file handling and provides various functions for reading, writing the files. The numpy module provides fromstring() method, and it is used to make a numpy array from a string. Now to convert a CSV file into numpy array, read the csv file using file handling. Then, for each row in the file, convert the row into numpy array using fromstring() method, and join all the arrays.

**Syntax of read_csv() function**

numpy.fromstring(str, sep)

- Parameters:
- str = A string containing the data..
- sep = The string separating numbers in the data by default the sep is a whitespace.

- Returns:
- Returns an numpy array.

**Syntax of open() function**

open(file, mode)

- Parameters:
- file = Name or path of the file to be loaded.
- mode = This specifies the access mode of opening a file by default the mode is read mode.

- Returns:
- Returns an file object.

**Approach:**

- Import numpy library.
- Open the csv file in read mode and read each row of the csv file.
- pass each row of the csv file and sep=”,” to the fromstring() method.
- the fromstring method will return a numpy array append it to a list
- Repeat step 3 and 4 till the last row of csv file.
- Convert the list into numpy array and print it.

**Source Code**

import numpy as np # Reading csv file into numpy array file_data = open('data.csv') l=[] for row in file_data: r = list(np.fromstring(row, sep=",")) l.append(r) # printing the array print(np.array(l))

**Output:**

[[1. 2. 3. 4. 5.] [6. 7. 8. 9. 0.] [2. 3. 4. 5. 6.] [4. 5. 6. 7. 7.]]

**Summary**

Great! you made it, We have discussed All possible methods to Read CSV file into a NumPy Array in Python. Happy learning.