I am currently using Spyder from Anaconda and I am trying to convert an array containing type float to type int:
x = np.array([1, 2, 2.5])
x.astype(int)
print x
The result still comes out unchanged:
[1. 2. 2.5]
Thoughts?
The astype() method returns a new DataFrame where the data types has been changed to the specified type. You can cast the entire DataFrame to one specific data type, or you can use a Python Dictionary to specify a data type for each column, like this: { 'Duration': 'int64', 'Pulse' : 'float', 'Calories': 'int64' }
The astype() function is used to cast a pandas object to a specified data type. Use a numpy. dtype or Python type to cast entire pandas object to the same type. Alternatively, use {col: dtype, …}, where col is a column label and dtype is a numpy.
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.
astype
returns a new array. You need to assign the result to x
:
In [298]: x = x.astype(int)
In [299]: x
Out[299]: array([1, 2, 2])
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