w3resource

How to read data from a CSV file into a structured NumPy array?

NumPy: I/O Operations Exercise-3 with Solution

Write a NumPy program that reads data from a CSV file into a structured NumPy array with specified data types for each column.

Content of data.csv
-------------------------
1,Jorah Liina,95.5
2,Basil Aisha,90.0
3,Helga Myrthe,80.0
4,Lehi Piero,91.5

Sample Solution:

Python Code:

import numpy as np

# Define the path to the CSV file
csv_file_path = 'data.csv'

# Define the data types for each column
dtype = [('ID', 'i4'), ('Name', 'U10'), ('Score', 'f4')]

# Read the CSV file into a structured NumPy array
structured_array = np.genfromtxt(csv_file_path, delimiter=',', dtype=dtype, names=True)

# Print the structured NumPy array
print(structured_array)

Output:

[(11, 'Zayden Jer', 90.) (12, 'Luna Marci', 85.) (13, 'Kord Shane', 96.)
 (14, 'Coeus Zora', 95.)]

Explanation:

  • Import the NumPy Library: Import the NumPy library to handle arrays.
  • Define CSV File Path: Specify the path to the CSV file containing the data.
  • Define Data Types: Define a list of tuples, where each tuple specifies the name and data type of a column. For example, 'i4' for a 4-byte integer, 'U10' for a Unicode string of length 10, and 'f4' for a 4-byte float.
  • Read CSV File into Structured Array: Use np.genfromtxt() to read the contents of the CSV file into a structured NumPy array, specifying the delimiter and the data types. The names=True parameter indicates that the first row of the CSV file contains column names.
  • Finally print the structured array.

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: How to write a NumPy array with Numeric and String data to a CSV file?
Next: How to write a structured NumPy array to a CSV file with proper formatting?

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.