w3resource

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


1. CSV Numeric Read

Write a NumPy program that reads data from a CSV file into a NumPy array. The CSV file contains numeric data.

Content of  data.csv file:
10,12,95
11,13,90
12,34,85
13,21,96
14,55,95

Sample Solution:

Python Code:

import numpy as np
import csv

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

# Initialize an empty list to hold the numeric data
data_list = []

# Open and read the CSV file
with open(csv_file_path, newline='') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for row in reader:
        # Extract only the numeric elements from the row
        numeric_row = []
        for element in row:
            try:
                numeric_value = float(element)
                numeric_row.append(numeric_value)
            except ValueError:
                # Skip non-numeric elements
                continue
        if numeric_row:
            data_list.append(numeric_row)

# Convert the list of lists to a NumPy array
data_array = np.array(data_list, dtype=object)

# Print the resulting NumPy array
print(data_array)

Output:

[[10.0 12.0 95.0]
 [11.0 13.0 90.0]
 [12.0 34.0 85.0]
 [13.0 21.0 96.0]
 [14.0 55.0 95.0]]

Explanation:

  • Import NumPy and CSV Libraries: Import the NumPy and CSV libraries to handle arrays and reading CSV files.
  • Define CSV File Path: Specify the path to the CSV file containing the numeric data.
  • Initialize Empty List: Create an empty list to store the valid numeric values from the CSV file.
  • Open and Read CSV File: Use csv.reader() to read the contents of the CSV file.
  • Extract Numeric Elements: Iterate through each row, attempting to convert each element to a float. Append valid numeric values to a temporary list for each row.
  • Append Non-Empty Rows: If the temporary list contains numeric values, append it to the main list.
  • Convert List to NumPy Array: Convert the list of numeric rows to a NumPy array using np.array(), specifying dtype=object to handle potential varying lengths of rows.
  • Print NumPy Array: Output the resulting NumPy array to verify the extracted numeric data.

For more Practice: Solve these Related Problems:

  • Write a Numpy program to read a CSV file containing only numeric data and then compute the sum of each column.
  • Write a Numpy program to load a CSV file of numeric values and then extract a sub-array based on a given row condition.
  • Write a Numpy program to import a CSV file with numeric data and then reshape the resulting array to a specified dimension.
  • Write a Numpy program to read numeric data from a CSV file and perform element-wise logarithmic transformation on the array.

Go to:


Previous:NumPy I/O Operations Exercises Home.
Next: How to write a NumPy array with Numeric and String data to a CSVfile?

Python-Numpy Code Editor:

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

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.