I have a csv file ready to load into my python code, however, I want to load it into the following format:
data = [[A,B,C,D],
[A,B,C,D],
[A,B,C,D],
]
How would I go about loading a .csv file that is readable as a numpy array? e.g., simply using previous tutorials plays havoc with using:
data = np.array(data)
Failing that, I would just like to upload my csv file (e.g. 'dual-Cored.csv' as data = dual-Cored.csv)
If your CVS looks like this:
then
would make
data
equal toAs a small example, I have some file
data.csv
with the following contents.Output
The simplest solution is just:
As long as the data is convertible into
float
and has an equal number of columns on each row, this works.If the data is not convertible into
float
in some column, you may write your own converters for it. Please see thenumpy.loadtxt
documentation. It is really very flexible.I'm assuming you mean to get all your data points as integers or floating point numbers.
First I wrote some sample data:
Now I'm reading back in the sample data
Which prints:
I recommend you read up a bit on datatypes in the Python tutorial, which talks about the difference between strings and numerical types.
To read into a numpy array with the csv module:
and
ar
now returns:Or directly use the
numpy.genfromtxt
function (you'll need to specify the delimiter):returns: