I have data stored in a CSV where the first row is strings (column names) and the remaining rows are numbers. How do I store this to a numpy array? All I can find is how to set data type for columns but not for rows.
Right now I’m just skipping the headers to do the calculations but I need to have the headers in the final version. But if I leave the headers in it sets the whole array as string and the calculations fail.
This is what I have:
data = np.genfromtxt(path_to_csv, dtype=None, delimiter=',', skip_header=1)
You can keep the column names if you use the
names=Trueargument in the functionnp.genfromtxtPlease note the
dtype=float, that will convert your data to float. This is more efficient than usingdtype=None, that asksnp.genfromtxtto guess the datatype for you.The output will be a structured array, where you can access individual columns by their name. The names will be taken from your first row. Some modifications may occur, spaces in a column name will be changed to
_for example. The documentation should cover most questions you could have.