I have a numpy matrix X, and I tried to change the datatype of column 1 using the code below:
X[:, 1].astype('str')
print(type(X[0, 1]))
but I got the following result:
<type 'numpy.float64'>
Anyone know why the type was not changed to str ? And what is a correct way to change the column type of X?Thanks!
To transpose NumPy array ndarray (swap rows and columns), use the T attribute ( . T ), the ndarray method transpose() and the numpy. transpose() function.
Rows and columns of NumPy arrays can be selected or modified using the square-bracket indexing notation in Python. To select a row in a 2D array, use P[i] . For example, P[0] will return the first row of P . To select a column, use P[:, i] .
In order to change the dtype of the given array object, we will use numpy. astype() function. The function takes an argument which is the target data type. The function supports all the generic types and built-in types of data.
Change column type in pandas using DataFrame.apply() to_numeric, pandas. to_datetime, and pandas. to_timedelta as arguments to apply the apply() function to change the data type of one or more columns to numeric, DateTime, and time delta respectively.
Providing a simple example will explain it better.
>>> a = np.array([[1,2,3],[4,5,6]])
array([[1, 2, 3],
[4, 5, 6]])
>>> a[:,1]
array([2, 5])
>>> a[:,1].astype('str') # This generates copy and then cast.
array(['2', '5'], dtype='<U21')
>>> a # So the original array did not change.
array([[1, 2, 3],
[4, 5, 6]])
More clear and straightforward answer. The type was not changed to str because NumPy array should have only one data type. The correct way to change the column type of X would be to use structured arrays or one of the solutions from this question.
I had the same problem, and I didn't want to use structured arrays. A possible option is to use pandas if it suits your task. If you're going to change just one column, possibly it means that your data is tabular. Then you can easily change the data type of column. Another compromise is to make a copy of the column and use it separately from the original array.
>>> x = np.ones((3, 3), dtype=np.float)
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
>>> x[:, 1] = x[:, 1].astype(np.int)
>>> type(x[:, 1][0])
numpy.float64
>>> x_pd = pd.DataFrame(x)
>>> x_pd[1] = x_pd[1].astype(np.int16)
>>> type(x_pd[1][0])
numpy.int16
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