[Numpy-discussion] Extracting data from genfromtxt?
Fri Mar 18 15:32:36 CDT 2011
On Fri, Mar 18, 2011 at 4:27 PM, Rohaq <firstname.lastname@example.org> wrote:
> I've got a CSV file with the following layout:
> ...etc., and I've loaded it using the following:
> import numpy as np
> data = np.genfromtxt('file.csv',delimiter=',',dtype=None,names=True)
> Resulting in the following:
> array([(1, 0, 0), (2, 0, 0), (3, 0, 0), ..., (16382, 0, 0), (16383, 0, 0),
> (16384, 0, 0)],
> dtype=[('Location', '<i8'), ('Result1', '<i8'), ('Result2', '<i8')])
> I want to plot the data in matplotlib.pyplot, but I'm having trouble
> reshaping the data into something it'll recognise. I can pull the
> Location easily enough with data['Location'], but I need to get the
> results from every other column present.
> There could be a lot more than 2 other columns, Result3,Result4, etc.,
> so I need to code it to be scalable; if this were acting like a normal
> dict, I could write a loop that iterates over the keys, and loads the
> results data into a new object, but I can't seem to find a function
> that will return the keys so that I can do this.
> Can anybody offer any tips for this? I'm fairly new to Python/Numpy,
> so any help is appreciated.
You've got a few options here. If you just want a plain array from
genfromtxt in the first place, then you can pass skip_headers=1, to
genfromtxt and it should will give you a plain array, if all the data
is of a homogeneous type.
If you want use your structured array, the most generic way to get an
ndarray (I think) is
You can replace float with int, if that's what you want.
More information about the NumPy-Discussion