How to read a CSV file with NumPy

CSV files often arrive as plain tables before their values are ready for array calculations. Reading one into NumPy works well when the rows and columns are regular enough to become numeric columns or a small structured array.

np.genfromtxt() reads delimited text into an array and can treat a header row as field names. It can also turn blank numeric cells into NaN so later checks such as np.isnan() and np.nanmean() behave predictably.

Use np.loadtxt() for clean numeric-only files, and use np.genfromtxt() when the CSV has headers, selected columns, or missing values. For quoted text, complex dates, or irregular rows, inspect the parsed table first or clean the file with a CSV-aware tool before relying on the NumPy array.

Steps to read a CSV file with NumPy:

  1. Create a small CSV file with a header row and one blank temperature field.
    readings.csv
    sensor,temp,humidity
    A,21.5,45
    B,,52
    C,19.0,49

    The blank field in row B gives the read step a missing numeric value to verify.

  2. Create a Python script that reads the CSV with named fields and fills blank numeric values with NaN.
    csv-read.py
    from pathlib import Path
     
    import numpy as np
     
    path = Path("readings.csv")
     
    data = np.genfromtxt(
        path,
        delimiter=",",
        names=True,
        dtype=None,
        encoding=None,
        missing_values="",
        filling_values=np.nan,
    )
     
    print("rows:", data.shape[0])
    print("columns:", data.dtype.names)
    print("temps:", data["temp"])
    print("missing temp:", np.isnan(data["temp"][1]))
    print("average temp:", np.nanmean(data["temp"]))

    dtype=None lets NumPy infer each named column independently. missing_values=“” and filling_values=np.nan convert the blank temperature cell into a numeric NaN.

  3. Run the script and verify the row count, field names, missing value, and numeric average.
    $ python3 csv-read.py
    rows: 3
    columns: ('sensor', 'temp', 'humidity')
    temps: [21.5  nan 19. ]
    missing temp: True
    average temp: 20.25

    The output proves that the header row became named fields and that the blank value stayed in the numeric temperature column as NaN.

  4. Remove the sample files after the check.
    $ rm readings.csv csv-read.py

    Keep real input files in place; this cleanup removes only the temporary files created for the local test.
    Related: Write CSV data