- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Replace NaN with zero and fill positive infinity values in Python
To replace NaN with zero and infinity with large finite numbers, use the numpy.nan_to_num() method in Python. The method returns, x, with the non-finite values replaced. If copy is False, this may be x itself. The 1st parameter is the input data. The 2nd parameter is copy, whether to create a copy of x (True) or to replace values in-place (False). The in-place operation only occurs if casting to an array does not require a copy. Default is True.
The 3rd parameter is nan, the value to be used to fill NaN values. If no value is passed then NaN values will be replaced with 0.0. The 4th parameter, posinf, a value to be used to fill positive infinity values. If no value is passed then positive infinity values will be replaced with a. The 5th parameter, neginfint, a value to be used to fill negative infinity values. If no value is passed then negative infinity values will be replaced with a very small (or negative) number.
Steps
At first, import the required libraries −
import numpy as np
Creating a numpy array using the array() method −
arr = np.array([np.inf, -np.inf, np.nan, -128, 128])
Display the array −
print("Our Array...
",arr)
Check the Dimensions −
print("
Dimensions of our Array...
",arr.ndim)
Get the Datatype −
print("
Datatype of our Array object...
",arr.dtype)
Get the Shape −
print("
Shape of our Array object...
",arr.shape)
To replace NaN with zero and infinity with large finite numbers, use the numpy.nan_to_num() method in Python −
print("
Result...
",np.nan_to_num(arr, posinf = 22222))
Example
import numpy as np # Creating a numpy array using the array() method arr = np.array([np.inf, -np.inf, np.nan, -128, 128]) # Display the array print("Our Array...
",arr) # Check the Dimensions print("
Dimensions of our Array...
",arr.ndim) # Get the Datatype print("
Datatype of our Array object...
",arr.dtype) # Get the Shape print("
Shape of our Array object...
",arr.shape) # To replace NaN with zero and infinity with large finite numbers, use the numpy.nan_to_num() method in Python print("
Result...
",np.nan_to_num(arr, posinf = 22222))
Output
Our Array... [ inf -inf nan -128. 128.] Dimensions of our Array... 1 Datatype of our Array object... float64 Shape of our Array object... (5,) Result... [ 2.22220000e+004 -1.79769313e+308 0.00000000e+000 -1.28000000e+002 1.28000000e+002]
- Related Articles
- Replace NaN with zero and fill positive infinity for complex input values in Python
- Replace NaN with zero and fill negative infinity values in Python
- Replace NaN with zero and fill negative infinity for complex input values in Python
- Replace infinity with large finite numbers but fill NaN values in Python
- Replace NaN with zero and infinity with large finite numbers in Python
- Replace NaN with zero and infinity with large finite numbers for complex input values in Python
- Replace infinity with large finite numbers and fill NaN for complex input values in Python
- Python Pandas - Fill missing columns values (NaN) with constant values
- Python - How to fill NAN values with mean in Pandas?
- Python Pandas - Fill NaN values with the specified value in an Index object
- NaN and Infinity example in JavaScript
- Python Pandas - Fill NaN values using an interpolation method
- Python - Replace negative values with latest preceding positive value in Pandas DataFrame
- Python Pandas - Fill NaN with Linear Interpolation
- Python Pandas - Fill NaN with Polynomial Interpolation
