
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Filter out integers from float numpy array
As part of data cleansing activities, we may sometimes need to take out the integers present in a list. In this article we will have an array containing both floats and integers. We will remove the integers from the array and print out the floats.
With astype
The astype function will be used to find if an element from the array is an integer or not. Accordingly we will decide to keep or remove the element from the array and store it in the result set.
Example
import numpy as np # initialising array A_array = np.array([3.2, 5.5, 2.0, 4.1,5]) print("Given array :\n ", A_array) # Only integers res = A_array[A_array != A_array.astype(int)] # result print("Array without integers:\n", res)
Output
Running the above code gives us the following result −
Given array : [3.2 5.5 2. 4.1 5. ] Array without integers: [3.2 5.5 4.1]
With equal and mod
In this approach we apply the mod function to each element of the array and check that on dividing the result is zero or not. If the result is not a zero then it is considered a float and kept as the result.
Example
import numpy as np # initialising array A_array = np.array([3.2, 5.5, 2.0, 4.1,5]) print("Given array :\n ", A_array) # Only integers res = A_array[~np.equal(np.mod(A_array, 1), 0)] # result print("Array without integers:\n", res)
Output
Running the above code gives us the following result −
Given array : [3.2 5.5 2. 4.1 5. ] Array without integers: [3.2 5.5 4.1]
- Related Articles
- Python – Filter Tuples with Integers
- JavaScript: How to filter out Non-Unique Values from an Array?
- Python - Filter Pandas DataFrame with numpy
- Convert Masked Array elements to Float Type in Numpy
- How to filter out common array in array of arrays in JavaScript
- How to select elements from Numpy array in Python?
- Filter null from an array in JavaScript?
- Remove/ filter duplicate records from array - JavaScript?
- Remove elements from array using JavaScript filter - JavaScript
- Return the default fill value for a masked array with float datatype in Numpy
- Python - Filter even values from a list
- How to convert Float array list to float array in Java?
- How to filter values from an array in TypeScript?
- How to filter an array from all elements of another array – JavaScript?
- C++ program to find out the maximum possible tally from given integers
