22.9 C
London
Monday, July 21, 2025
HomePandas in PythonGeneral Functions in PythonHow to Convert NumPy Array of Floats into Integers

How to Convert NumPy Array of Floats into Integers

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following methods to convert a NumPy array of floats to an array of integers:

Method 1: Convert Floats to Integers (Rounded Down)

rounded_down_integer_array = float_array.astype(int)

Method 2: Convert Floats to Integers (Rounded to Nearest Integer)

rounded_integer_array = (np.rint(some_floats)).astype(int)

Method 3: Convert Floats to Integers (Rounded Up)

rounded_up_integer_array = (np.ceil(float_array)).astype(int)

The following examples show how to use each method in practice with the following NumPy array of floats:

import numpy as np

#create NumPy array of floats
float_array = np.array([2.33, 4.7, 5.1, 6.2356, 7.88, 8.5])

#view array
print(float_array)

[2.33   4.7    5.1    6.2356 7.88   8.5   ]

#view dtype of array
print(float_array.dtype)

float64

Example 1: Convert Floats to Integers (Rounded Down)

The following code shows how to convert a NumPy array of floats to an array of integers in which each float is rounded down to the nearest integer:

#convert NumPy array of floats to array of integers (rounded down)
rounded_down_integer_array = float_array.astype(int)

#view array
print(rounded_down_integer_array)

[2 4 5 6 7 8]

#view dtype of array
print(rounded_down_integer_array.dtype)

int32

Notice that each float has been rounded down to the nearest integer and the new array has a dtype of int32.

Example 2: Convert Floats to Integers (Rounded to Nearest Integer)

The following code shows how to convert a NumPy array of floats to an array of integers in which each float is rounded to the nearest integer:

#convert NumPy array of floats to array of integers (rounded to nearest)
rounded_integer_array = (np.rint(float_array)).astype(int)

#view array
print(rounded_integer_array)

[2 5 5 6 8 8]

#view dtype of array
print(rounded_integer_array.dtype)

int32

Notice that each float has been rounded to the nearest integer and the new array has a dtype of int32.

Example 3: Convert Floats to Integers (Rounded Up)

The following code shows how to convert a NumPy array of floats to an array of integers in which each float is rounded up to the nearest integer:

#convert NumPy array of floats to array of integers (rounded up)
rounded_up_integer_array = (np.ceil(float_array)).astype(int)

#view array
print(rounded_up_integer_array)

[3 5 6 7 8 9]

#view dtype of array
print(rounded_up_integer_array.dtype)

int32

Notice that each float has been rounded up to the nearest integer and the new array has a dtype of int32.

Additional Resources

The following tutorials explain how to perform other common tasks in NumPy:

How to Fill NumPy Array with Values
How to Remove Specific Elements from NumPy Array
How to Replace Elements in NumPy Array
How to Get Specific Row from NumPy Array

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories