Create an account


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 'my_file.csv').

INPUT: file 'my_file.csv'
9,8,7
6,5,4
3,2,1
How to Convert a CSV to NumPy Array in Python?

Challenge: How to convert it to a NumPy Array?

OUTPUT: 2D NumPy Array
[[9. 8. 7.] [6. 5. 4.] [3. 2. 1.]]

Method 1: np.loadtxt()


np.loadtxt()

You can convert a CSV file to a NumPy array simply by calling np.loadtxt() with two arguments: the filename and the delimiter string. For example, the expression np.loadtxt('my_file.csv', delimiter=',') returns a NumPy array from the 'my_file.csv' with delimiter symbols ','.

Here’s an example:

import numpy as np array = np.loadtxt('my_file.csv', delimiter=',')
print(array)

Output:

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

Method 2: np.loadtxt() with Header


np.loadtxt() + header

You can convert a CSV file with first-line header to a NumPy array by calling np.loadtxt() with three arguments: the filename, skiprows=1 to skip the first line (header), and the delimiter string. For example, the expression np.loadtxt('my_file.csv', skiprows=1, delimiter=',') returns a NumPy array from the 'my_file.csv' with delimiter symbols ',' while skipping the first line.

Figure: Skip the first header line in the CSV using the skiprows argument of the np.loadtxt() function.

Here’s an example:

import numpy as np array = np.loadtxt('my_file.csv', skiprows=1, delimiter=',')
print(array)

Output:

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

Method 3: CSV Reader


CSV Reader

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

Here’s an example:

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:

[['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 np.array() function on the newly-created list like so:

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

Output:

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

? Related Tutorial: How to Convert CSV to List of Lists in Python

Method 4: np.genfromtxt()


np.genfromtxt()

You can convert a CSV file to a NumPy array simply by calling np.genfromtxt() with two arguments: the filename and the delimiter string. For example, the expression np.genfromtxt('my_file.csv', delimiter=',') returns a NumPy array from the 'my_file.csv' with delimiter symbol ','.

Here’s an example:

import numpy as np array = np.loadtxt('my_file.csv', delimiter=',')
print(array)

Output:

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

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


read_csv() and df.to_numpy()

A quick and efficient way to read a CSV to a NumPy array is to combine Pandas’ pd.read_csv() function to read a given CSV file to a DataFrame with the df.to_numpy() function to convert the Pandas DataFrame to a NumPy array.

Here’s an example:

import pandas as pd df = pd.read_csv('my_file.csv', header=None)
array = df.to_numpy() print(array)

Output:

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

? 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: np.loadtxt()
  • Method 2: np.loadtxt() with Header
  • Method 3: CSV Reader
  • Method 4: np.genfromtxt()
  • Method 5: Pandas read_csv() and df.to_numpy()

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



https://www.sickgaming.net/blog/2022/07/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Convert MIDI to MP3 in Python – A Quick Overview xSicKxBot 0 2,391 09-02-2023, 02:04 PM
Last Post: xSicKxBot
  [Tut] How to Convert an Octal Escape Sequence in Python – And Vice Versa? xSicKxBot 0 1,329 12-08-2022, 01:23 PM
Last Post: xSicKxBot
  [Tut] How to Convert Octal String to Integer in Python xSicKxBot 0 1,295 12-04-2022, 08:39 AM
Last Post: xSicKxBot
  [Tut] Python Convert Hex to Base64 xSicKxBot 0 1,276 11-30-2022, 09:32 PM
Last Post: xSicKxBot
  [Tut] Python Library Hijacking – A Simple Demonstration on NumPy xSicKxBot 0 1,248 11-22-2022, 01:48 AM
Last Post: xSicKxBot
  [Tut] Solidity Bytes and String Arrays, Concat, Allocating Memory, and Array Literals xSicKxBot 0 1,199 10-25-2022, 09:13 AM
Last Post: xSicKxBot
  [Tut] How to Convert Pandas DataFrame/Series to NumPy Array? xSicKxBot 0 1,213 10-24-2022, 02:13 PM
Last Post: xSicKxBot
  [Tut] How to Print a NumPy Array Without Scientific Notation in Python xSicKxBot 0 1,216 10-20-2022, 11:44 AM
Last Post: xSicKxBot
  [Tut] Python – Return NumPy Array From Function xSicKxBot 0 1,183 10-16-2022, 03:49 AM
Last Post: xSicKxBot
  [Tut] How to Convert Bool (True/False) to a String in Python? xSicKxBot 0 1,302 10-04-2022, 11:37 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016