How to read a CSV file with missing values into a NumPy array?
NumPy: I/O Operations Exercise-5 with Solution
Write a NumPy program that reads a CSV file containing missing values into a NumPy array, handling the missing values appropriately.
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'
# Read the CSV file into a NumPy array, handling missing values as NaN
data_array = np.genfromtxt(csv_file_path, delimiter=',', dtype=float, filling_values=np.nan)
# Print the resulting NumPy array
print(data_array)
Output:
[[10. nan 95.] [11. nan 90.] [12. nan 85.] [13. nan 96.] [14. nan 95.]]
Explanation:
- Import NumPy Library: Import the NumPy library to handle arrays.
- Define CSV File Path: Specify the path to the CSV file containing the data with missing values.
- Read CSV File into NumPy Array: Use np.genfromtxt() to read the contents of the CSV file into a NumPy array. The delimiter parameter specifies the delimiter used in the CSV file, dtype=float ensures all data is treated as floating-point numbers, and filling_values=np.nan fills missing values with NaN.
- Print NumPy Array: Output the resulting NumPy array to verify the data read from the CSV file, with missing values appropriately handled.
Python-Numpy Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: How to write a structured NumPy array to a CSV file with proper formatting?
Next: How to save a NumPy array to a text file and read it back?
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://198.211.115.131/python-exercises/numpy/read-a-csv-file-with-missing-values-into-a-numpy-array.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics