As we have seen in the preceding sections, NumPy supports multiple data types, such as int, float, and complex numbers. The astype() function converts the data type of the array. Let's see an example of the astype() function:
# Create an array
arr=np.arange(1,10).reshape(3,3)
print("Integer Array:",arr)
# Change datatype of array
arr=arr.astype(float)
# print array
print("Float Array:", arr)
# Check new data type of array
print("Changed Datatype:", arr.dtype)
In the preceding code, we have created one NumPy array and checked its data type using the dtype attribute.
Let's change the data type of an array using the astype() function:
# Change datatype of array
arr=arr.astype(float)
# Check new data type of array
print(arr.dtype)
Output:
float64
In the preceding code, we have changed the column data type from integer to float using astype().
The tolist() function converts a NumPy array into a Python list. Let's see an...