- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Compare two arrays with some NaN values and return the element-wise minimum in Numpy

To compare two arrays with some NaN values and return the element-wise minimum, use the **numpy.maximum()** method in Python Numpy

If one of the elements being compared is a NaN, then that element is returned.

If both elements are NaNs then the first is returned

The latter distinction is important for complex NaNs, which are defined as at least one of the real or imaginary parts being a NaN.

The net effect is that NaNs are propagated.

Returns the minimum of x1 and x2, element-wise. This is a scalar if both x1 and x2 are scalars.

Compare two arrays and returns a new array containing the element-wise minima. If one of the elements being compared is a NaN, then that element is returned. If both elements are NaNs then the first is returned. The latter distinction is important for complex NaNs, which are defined as at least one of the real or imaginary parts being a NaN. The net effect is that NaNs are propagated.

## Steps

At first, import the required library −

import numpy as np

Creating two 2D numpy array using the array() method. We have inserted elements with some nan values −

arr1 = np.array([[6, np.nan, np.nan],[25, 11, 0]]) arr2 = np.array([[8, 12, np.nan],[22, 0, 26]])

Display the arrays −

print("Array 1...\n", arr1) print("\nArray 2...\n", arr2)

Get the type of the arrays −

print("\nOur Array 1 type...\n", arr1.dtype) print("\nOur Array 2 type...\n", arr2.dtype)

Get the dimensions of the Arrays −

print("\nOur Array 1 Dimensions...\n",arr1.ndim) print("\nOur Array 2 Dimensions...\n",arr2.ndim)

Get the shape of the Arrays −

print("\nOur Array 1 Shape...\n",arr1.shape) print("\nOur Array 2 Shape...\n",arr2.shape)

To compare two arrays with some NaN values and return the element-wise minimum, use the numpy.maximum() method −

print("\nResult (minimum)...\n",np.minimum(arr1, arr2))

## Example

import numpy as np # Creating two 2D numpy array using the array() method # We have inserted elements with some nan values arr1 = np.array([[6, np.nan, np.nan], [25, 11, 0]]) arr2 = np.array([[8, 12, np.nan],[22, 0, 26]]) # Display the arrays print("Array 1...\n", arr1) print("\nArray 2...\n", arr2) # Get the type of the arrays print("\nOur Array 1 type...\n", arr1.dtype) print("\nOur Array 2 type...\n", arr2.dtype) # Get the dimensions of the Arrays print("\nOur Array 1 Dimensions...\n",arr1.ndim) print("\nOur Array 2 Dimensions...\n",arr2.ndim) # Get the shape of the Arrays print("\nOur Array 1 Shape...\n",arr1.shape) print("\nOur Array 2 Shape...\n",arr2.shape) # To compare two arrays with some NaN values and return the elementwise minimum, use the numpy.maximum() method in Python Numpy # If one of the elements being compared is a NaN, then that element is returned. # If both elements are NaNs then the first is returned. # The latter distinction is important for complex NaNs, which are defined as at least one of the real or imaginary parts being a NaN. # The net effect is that NaNs are propagated. print("\nResult (minimum)...\n",np.minimum(arr1, arr2))

## Output

Array 1... [[ 6. nan nan] [25. 11. 0.]] Array 2... [[ 8. 12. nan] [22. 0. 26.]] Our Array 1 type... float64 Our Array 2 type... float64 Our Array 1 Dimensions... 2 Our Array 2 Dimensions... 2 Our Array 1 Shape... (2, 3) Our Array 2 Shape... (2, 3) Result (minimum)... [[ 6. nan nan] [22. 0. 0.]]

- Related Questions & Answers
- Compare two Numpy arrays with some Inf values and return the element-wise minimum
- Compare two Numpy arrays and return the element-wise minimum with fmin()
- Compare two arrays and return the element-wise minimum in Numpy
- Compare two arrays with some Inf values and return the element-wise maximum in Numpy
- Compare two Numpy arrays and return the element-wise minimum ignoring NaNs
- Compare two Numpy arrays and return the element-wise maximum with fmax()
- Compare two arrays and return the element-wise maximum in Numpy
- Compare two arrays and return the element-wise maximum ignoring NaNs in Numpy
- Compute the bit-wise AND of two arrays element-wise in Numpy
- Compute the bit-wise AND of two Two-Dimensional arrays element-wise in Numpy
- Compute the bit-wise AND of two boolean arrays element-wise in Numpy
- Compute the bit-wise XOR of two Numpy arrays element-wise
- Compute the bit-wise OR of two Numpy arrays element-wise
- Compute the bit-wise AND of two One-Dimensional Numpy arrays element-wise
- Return element-wise string concatenation for two arrays of string in Numpy