# 5 examples of 'numpy replace nan with 0' in Python

Every line of 'numpy replace nan with 0' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

## All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
``135def replace_nan(value, default=0):136    if math.isnan(value):137        return default138    return value``
``36def _replace_nan(a, val):37    """38    If `a` is of inexact type, make a copy of `a`, replace NaNs with39    the `val` value, and return the copy together with a boolean mask40    marking the locations where NaNs were present. If `a` is not of41    inexact type, do nothing and return `a` together with a mask of None.4243    Note that scalars will end up as array scalars, which is important44    for using the result as the value of the out argument in some45    operations.4647    Parameters48    ----------49    a : array-like50        Input array.51    val : float52        NaN values are set to val before doing the operation.5354    Returns55    -------56    y : ndarray57        If `a` is of inexact type, return a copy of `a` with the NaNs58        replaced by the fill value, otherwise return `a`.59    mask: {bool, None}60        If `a` is of inexact type, return a boolean mask marking locations of61        NaNs, otherwise return None.6263    """64    a = np.array(a, subok=True, copy=True)6566    if a.dtype == np.object_:67        # object arrays do not support `isnan` (gh-9009), so make a guess68        mask = a != a69    elif issubclass(a.dtype.type, np.inexact):70        mask = np.isnan(a)71    else:72        mask = None7374    if mask is not None:75        np.copyto(a, val, where=mask)7677    return a, mask``
``24def fix_nans(mat):25    """26    returns the matrix with average over models if a model, sample, chromosome had nan in it.27    :param mat: ndarray (model, sample, chromosome)28    :return: mat ndarray (model, sample, chromosome)29    """30    mat = np.nan_to_num(mat)31    idx, idy, idz = np.where(mat == 0)32    for x, y, z in zip(idx, idy, idz):33        mat[x, y, z] = mat[:, y, z].mean()34    return mat``
``74def filter_nan2none(value):75    """Convert the NaN value to None, leaving everything else unchanged.7677    This function is meant to be used as a Django template filter. It78    is useful in combination with filters that handle None (or any79    false value) specially, such as the 'default' filter, when one80    wants special treatment for the NaN value. It is also useful81    before the 'format' filter to avoid the NaN value being formatted.8283    """84    if is_nan(value):85        return None86    return value``
``203def nan_to_zero(segment: Union[pd.Series, list], nan_list: list) -&gt; Union[pd.Series, list]:204    if type(segment) == pd.Series:205        for val in nan_list:206            segment.values[val] = 0207    else:208        for val in nan_list:209            segment[val] = 0210    return segment``