How can I read CSV data and convert it into a record array using NumPy in Python?

I’m trying to read a CSV file into a NumPy record array, but I’m having trouble figuring out the correct syntax. My CSV file contains columns with headers, and I want to be able to access the data using those headers as field names in the record array. Can someone provide me with an example code snippet that shows how to do this?

Here’s the code I have so far:

import numpy as np

data = np.genfromtxt('my_data.csv', delimiter=',', dtype=None, names=True)

However, when I try to access the data using the field names, I get an error:


Error message: IndexError: field not found: column1

Can anyone help me figure out what I’m doing wrong? Is there a different way to read in the CSV file that would allow me to access the data using the column headers as field names?