How to read numeric data from a CSV file into a NumPy array?
NumPy: I/O Operations Exercise-1 with Solution
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.
Python-Numpy Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous:NumPy I/O Operations Exercises Home.
Next: How to write a NumPy array with Numeric and String data to a CSVfile?
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/numpy/read-numeric-data-from-a-csv-file-into-a-numpy-array.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics