I have to do it without using pandas or something else, just pure numpy
I have a big ndarray
of numpy.str_
read from the CSV file, I'd like to convert every element of every column to the particular type. For instance, I know that in the 2nd column there will be an integer and I would like to convert all elements of the first column to numpy.float
, every element of the 2nd column to numpy.int
and so on, is there a way how to elegantly implement it?
Is there a way how to specify what element should be converted to what type when I convert list
to ndarray
with np.array()
, somehow to play with the dtype?
I tried to find something and found this https://numpy.org/doc/stable/user/basics.rec.html#structured-arrays but I don't think that it is what I'm looking for. In case it is, could explain me how to use it in my case.
Appreciate your help.
Example:
I have [['1', '1.6', 'hey']['2', '5', 'tr5']]
How do I get something like
array([1, 1.6, 'hey'][2, 5.0, 'tr5'])
CodePudding user response:
Yes, structured arrays is what you need.
You have to pass the dtype
argument as follow:
np.array(data, dtype=[(name1, type1), (name2, type2), ...])
Where data is a list of tuples with fields.
You have to convert the original array to that structure.
If you dont need the names just put empty str.
you also can put dtype argument as dtype=np.dtype(type1, type2,...)