Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Convert a CSV to NumPy Array in Python?
1
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
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]

Challenge: How to convert it to a NumPy Array?

Code:
OUTPUT: 2D NumPy Array [[9. 8. 7.] [6. 5. 4.] [3. 2. 1.]]
Method 1: np.loadtxt()

[Image: python-logo-generic-reddit-1024x536.jpg]

np.loadtxt()

You can convert a CSV file to a NumPy array simply by calling
Code:
np.loadtxt()
with two arguments: the
Code:
filename
and the
Code:
delimiter
string. For example, the expression
Code:
np.loadtxt('my_file.csv', delimiter=',')
returns a NumPy array from the
Code:
'my_file.csv'
with delimiter symbols
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.]]
Method 2: np.loadtxt() with Header

[Image: 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()
with three arguments: the
Code:
filename
,
Code:
skiprows=1
to skip the first line (header), and the
Code:
delimiter
string. For example, the expression
Code:
np.loadtxt('my_file.csv', skiprows=1, delimiter=',')
returns a NumPy array from the
Code:
'my_file.csv'
with delimiter symbols
Code:
','
while skipping the first line.

[Image: image-51.png][i]Figure: Skip the first header line in the CSV using the
Code:
skiprows
argument of the
Code:
np.loadtxt()
function.
[/i]

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.]]
Method 3: CSV Reader

[Image: python-logo-generic-reddit-1024x536.jpg]

CSV Reader

To convert a CSV file
Code:
'my_file.csv'
into a list of lists in Python, use the
Code:
csv.reader(file_obj)
method to create a CSV file reader. Then convert the resulting object to a list using the
Code:
list()
constructor. As a final step, you can convert the nested list to a NumPy array by using the
Code:
np.array(list)
constructor.

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()
function on the newly-created list like so:

Code:
array = np.array(lst) print(array)

Output:

Code:
[['9' '8' '7'] ['6' '5' '4'] ['3' '2' '1']]

[Image: 1f30e.png] Related Tutorial: How to Convert CSV to List of Lists in Python

Method 4: np.genfromtxt()

[Image: python-logo-generic-reddit-1024x536.jpg]

np.genfromtxt()

You can convert a CSV file to a NumPy array simply by calling
Code:
np.genfromtxt()
with two arguments: the
Code:
filename
and the
Code:
delimiter
string. For example, the expression
Code:
np.genfromtxt('my_file.csv', delimiter=',')
returns a NumPy array from the
Code:
'my_file.csv'
with delimiter symbol
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.]]
Method 5: Pandas read_csv() and df.to_numpy()

[Image: 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()
function to read a given CSV file to a DataFrame with the
Code:
df.to_numpy()
function to convert the Pandas DataFrame to a NumPy array.

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]]

[Image: 1f30e.png] 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:
  • Method 1:
    Code:
    np.loadtxt()

  • Method 2:
    Code:
    np.loadtxt()
    with Header

  • Method 3: CSV Reader

  • Method 4:
    Code:
    np.genfromtxt()

  • Method 5: Pandas
    Code:
    read_csv()
    and
    Code:
    df.to_numpy()

Our preferred way is
Code:
np.loadtxt()
for its simplicity and Pandas for its extensibility.
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: