# How to convert NumPy array float64 to int

In this post, we are going to learn how to convert NumPy array float64 to int with examples by using the NumPy library built-in function astype(), int_(), and NumPy.asarray(). Whenever we pass a float type element to a function that expects an integer TypeError: ‘numpy.float64’ object cannot be interpreted as an integer. To solve this problem we have to convert the float type to int and pass it to the function. To run all programs in this post, Make sure the NumPy library is already installed on the local machine.

## How to convert NumPy array float64 to int

• astype(): takes dtype=’int’ to convert array of float to int.
• int_(): take array as agrument and convert into int
• asarray() : Convert given data to desired type by using dtype=”” agrument
• int() : To loop through array convert each element to int

### 1. Convert NumPy array float64 to int using astype()

NumPy.astype() function is used to change the data type float64 to int and to use this function first, We have to import the NumPy library in our program by using the code “import NumPy as np”.To convert a NumPy array of float to int We have passed a NumPy float array with dtype= int in the astype() function. The Numpy 1D or any dimensional array converts float to int this way.

• The existing datatype of the given NumPy array is ‘float’.We can check datatype using by dtype attribute of the NumPy array.
• To change the datatype of the existing NumPy array we have used numpy.astype() function passed datatype int as argument.
```import numpy as np
np2Darr = np.array([[ 3.5 , 6.1 , 9.0, 12.5], [11.1, 13.2, 18.2, 21.2]])

print('existing datatype:',np2Darr.dtype)
nparr = np2Darr.astype(int)

print('changed datatype:',nparr.dtype)
print('\nnumpy array float to int:\n',nparr)
```

Output

```numpy array float to int:
[[ 3.5  6.1  9.  12.5]
[11.1 13.2 18.2 21.2]]
```

### 2. Convert NumPy array float64 to int using int_()

Another way to convert NumPy array float64 to int is by using np.int_() function. We have to pass the NumPy array of float as an argument to this function and it will return a NumPy array of int. Let’s understand with the below example.

```import numpy as np
np2Darr = np.array([[ 3.5 , 6.1 , 9.0, 12.5], [11.1, 13.2, 18.2, 21.2]])

nparr = np.int_(np2Darr)

print('numpy array float to int:\n',nparr)

```

Output

```numpy array float to int:
[[ 3  6  9 12]
[11 13 18 21]]
```

### 3. Convert NumPy array float64 to int using asarray()

In this Python program example, we are using the np.asarray() function to convert the NumPy array of float to int. The np.asarray() function converts input data tuple, array, list, tuples of the list, and a tuple of tuples into an array. So can specify the datatype by using dtype=”int” while converting as in the below example.

```import numpy as np
np2Darr = np.array([[ 3.5 , 6.1 , 9.0, 12.5], [11.1, 13.2, 18.2, 21.2]])

nparr = np.asarray(np2Darr,dtype="int")

print('Converted array:\n',nparr)

```

Output

```Converted array:
[[ 3  6  9 12]
[11 13 18 21]]
```

### 4. Convert 1D NumPy array float64 to int using int()

We can convert a 1D numpy array of float to int by using the Python built-in function int().In this example, we are looping through an array by using a range loop in Python, and one iteration converts the numpy array element to int and appending to a list. Finally converted the List to a numpy array using np.array() function.

```import numpy as np
np2Darr = np.array([ 3.5 , 6.1 , 9.0, 12.5, 11.1, 13.2, 18.2, 21.2])

mylist=[]
for item in range(len(np2Darr)):
mylist.append(int(np2Darr[item]))

resarr = np.array(mylist)
print('Result array:',resarr)
print('datatype of converted array:\n',resarr.dtype)

```

Output

```Result array: [ 3  6  9 12 11 13 18 21]
datatype of converted array:
int32
```

### Summary

In this post, we have learned how to convert NumPy array float64 to int with example by using different numpy and python built-in functions astype(), int_(), and NumPy.asarray(), int()