07-11-2022, 03:42 AM
How to Convert a CSV to NumPy Array in Python?
5/5 – (1 vote)
Problem Formulation
Given a CSV file (e.g., stored in the file with name).
![[Image: convert_csv_nparray-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2022/07/convert_csv_nparray-1024x576.jpg)
Challenge: How to convert it to a NumPy Array?
Method 1: np.loadtxt()
![[Image: python-logo-generic-reddit-1024x536.jpg]](https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg)
np.loadtxt()
You can convert a CSV file to a NumPy array simply by calling with two arguments: the and the string. For example, the expression returns a NumPy array from the with delimiter symbols .
Here’s an example:
Output:
Method 2: np.loadtxt() with Header
![[Image: python-logo-generic-reddit-1024x536.jpg]](https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg)
np.loadtxt() + header
You can convert a CSV file with first-line header to a NumPy array by calling with three arguments: the , to skip the first line (header), and the string. For example, the expression returns a NumPy array from the with delimiter symbols while skipping the first line.
[i]Figure: Skip the first header line in the CSV using the argument of the function.[/i]
Here’s an example:
Output:
Method 3: CSV Reader
![[Image: python-logo-generic-reddit-1024x536.jpg]](https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg)
CSV Reader
To convert a CSV file into a list of lists in Python, use the method to create a CSV file reader. Then convert the resulting object to a list using the constructor. As a final step, you can convert the nested list to a NumPy array by using the constructor.
Here’s an example:
The output is the list of lists:
Now, if you need to convert it to a NumPy array, you can simply use the function on the newly-created list like so:
Output:
Related Tutorial: How to Convert CSV to List of Lists in Python
Method 4: np.genfromtxt()
![[Image: python-logo-generic-reddit-1024x536.jpg]](https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg)
np.genfromtxt()
You can convert a CSV file to a NumPy array simply by calling with two arguments: the and the string. For example, the expression returns a NumPy array from the with delimiter symbol .
Here’s an example:
Output:
Method 5: Pandas read_csv() and df.to_numpy()
![[Image: python-logo-generic-reddit-1024x536.jpg]](https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg)
read_csv() and df.to_numpy()
A quick and efficient way to read a CSV to a NumPy array is to combine Pandas’ function to read a given CSV file to a DataFrame with the function to convert the Pandas DataFrame to a NumPy array.
Here’s an example:
Output:
Related Tutorial: 17 Ways to Read a CSV File to a Pandas DataFrame
Summary
We have seen five ways to convert a CSV file to a 2D NumPy array:
Our preferred way is for its simplicity and Pandas for its extensibility.
5/5 – (1 vote)
Problem Formulation
Given a CSV file (e.g., stored in the file with name
Code:
'my_file.csv'Code:
INPUT: file 'my_file.csv'
9,8,7
6,5,4
3,2,1![[Image: convert_csv_nparray-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2022/07/convert_csv_nparray-1024x576.jpg)
Challenge: How to convert it to a NumPy Array?
Code:
OUTPUT: 2D NumPy Array
[[9. 8. 7.] [6. 5. 4.] [3. 2. 1.]]![[Image: python-logo-generic-reddit-1024x536.jpg]](https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg)
np.loadtxt()
You can convert a CSV file to a NumPy array simply by calling
Code:
np.loadtxt()Code:
filenameCode:
delimiterCode:
np.loadtxt('my_file.csv', delimiter=',')Code:
'my_file.csv'Code:
','Here’s an example:
Code:
import numpy as np array = np.loadtxt('my_file.csv', delimiter=',')
print(array)Output:
Code:
[[9. 8. 7.] [6. 5. 4.] [3. 2. 1.]]![[Image: python-logo-generic-reddit-1024x536.jpg]](https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg)
np.loadtxt() + header
You can convert a CSV file with first-line header to a NumPy array by calling
Code:
np.loadtxt()Code:
filenameCode:
skiprows=1Code:
delimiterCode:
np.loadtxt('my_file.csv', skiprows=1, delimiter=',')Code:
'my_file.csv'Code:
','
[i]Figure: Skip the first header line in the CSV using the Code:
skiprowsCode:
np.loadtxt()Here’s an example:
Code:
import numpy as np array = np.loadtxt('my_file.csv', skiprows=1, delimiter=',')
print(array)Output:
Code:
[[9. 8. 7.] [6. 5. 4.] [3. 2. 1.]]![[Image: python-logo-generic-reddit-1024x536.jpg]](https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg)
CSV Reader
To convert a CSV file
Code:
'my_file.csv'Code:
csv.reader(file_obj)Code:
list()Code:
np.array(list)Here’s an example:
Code:
import numpy as np
import csv csv_filename = 'my_file.csv' with open(csv_filename) as f: reader = csv.reader(f) lst = list(reader) print(lst)The output is the list of lists:
Code:
[['9', '8', '7'], ['6', '5', '4'], ['3', '2', '1']]Now, if you need to convert it to a NumPy array, you can simply use the
Code:
np.array()Code:
array = np.array(lst)
print(array)Output:
Code:
[['9' '8' '7'] ['6' '5' '4'] ['3' '2' '1']]
Related Tutorial: How to Convert CSV to List of Lists in PythonMethod 4: np.genfromtxt()
![[Image: python-logo-generic-reddit-1024x536.jpg]](https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg)
np.genfromtxt()
You can convert a CSV file to a NumPy array simply by calling
Code:
np.genfromtxt()Code:
filenameCode:
delimiterCode:
np.genfromtxt('my_file.csv', delimiter=',')Code:
'my_file.csv'Code:
','Here’s an example:
Code:
import numpy as np array = np.loadtxt('my_file.csv', delimiter=',')
print(array)Output:
Code:
[[9. 8. 7.] [6. 5. 4.] [3. 2. 1.]]![[Image: python-logo-generic-reddit-1024x536.jpg]](https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg)
read_csv() and df.to_numpy()
A quick and efficient way to read a CSV to a NumPy array is to combine Pandas’
Code:
pd.read_csv()Code:
df.to_numpy()Here’s an example:
Code:
import pandas as pd df = pd.read_csv('my_file.csv', header=None)
array = df.to_numpy() print(array)Output:
Code:
[[9 8 7] [6 5 4] [3 2 1]]
Related Tutorial: 17 Ways to Read a CSV File to a Pandas DataFrameSummary
We have seen five ways to convert a CSV file to a 2D NumPy array:
- Method 1: Code:
np.loadtxt()
- Method 2: with HeaderCode:
np.loadtxt()
- Method 3: CSV Reader
- Method 4: Code:
np.genfromtxt()
- Method 5: Pandas andCode:
read_csv()Code:df.to_numpy()
Our preferred way is
Code:
np.loadtxt()
