w3resource

How to read a CSV file with missing values into a NumPy array?


5. CSV with Missing Values Read

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.

For more Practice: Solve these Related Problems:

  • Write a Numpy program to read a CSV file containing missing values and then fill these gaps with the column mean.
  • Write a Numpy program to load a CSV file with missing numeric entries and then interpolate the missing values using linear interpolation.
  • Write a Numpy program to import a CSV file with missing data and then apply a forward-fill technique to complete the array.
  • Write a Numpy program to read CSV data with missing entries and then use masking to compute statistics only on non-missing values.

Go to:


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?

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.