How to Convert Data Type From Float to Integer in NumPy


In this example we will show how to convert data type from float to integer by using int as parameter value in Python NumPy.

Source Code

import numpy as np

n = np.array([1.2, 3.4, 5.6, 7.8])
float_to_int = n.astype(int)

print(float_to_int)
print(float_to_int.dtype)

Output:

[1 3 5 7]
int32
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

How to Convert Data Type From Float to Integer in NumPy


In this example we will show how to convert data type from float to integer by using ‘i’ as parameter value in Python NumPy.

Source Code

import numpy as np

n = np.array([1.2, 3.4, 5.6, 7.8])
float_to_int = n.astype('i')

print(float_to_int)
print(float_to_int.dtype)

Output:

[1 3 5 7]
int32
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments