How can I have genfromtxt
to return me its list
of column names which were automatically retrieved by names=True
? When I do:
data = np.genfromtxt("test.csv",names=True,delimiter=",",dtype=None)
print data['col1']
it prints the entire column values for col1
.
However, I need to traverse all column names. How can I do that?
I tried data.keys()
and various other methods, but whatever is returned by genfromtxt
does not seem to be a dictionary compatible object. I guess I could pass the list
of column names myself, but this won't be maintainable for me in the long run.
Any ideas?
genfromtxt. Load data from a text file, with missing values handled as specified. Each line past the first skip_header lines is split at the delimiter character, and characters following the comments character are discarded.
The only mandatory argument of genfromtxt is the source of the data. It can be a string, a list of strings, a generator or an open file-like object with a read method, for example, a file or io. StringIO object.
delimiter : The string used to separate values. By default, this is any whitespace. converters : A dictionary mapping column number to a function that will convert that column to a float. E.g., if column 0 is a date string: converters = {0: datestr2num}.
genfromtxt
returns a numpy.ndarray.
You can get the data type with
data.dtype
or just the names with
data.dtype.names
which is a tuple you can iterate over and access the columns as you want to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With