'How to replace specific values in a list with NaN?

I have the following list:

data = [[1, 2, 3, 4], [0, 2, 3, 4], [0, 0 , 3, 4], [0, 0, 0, 4]]

I am trying to replace the 0 values with NaN/null, such that the list is transformed into the following:

data = [[1, 2, 3, 4], [NaN, 2, 3, 4], [NaN, NaN , 3, 4], [NaN, NaN, NaN, 4]]

I have tried searching google and stack overflow but I couldn't seem to find a solution. I have managed to convert the values into NaN after first representing 'data' as a dataframe, but for my purposes this is not sufficient as I need the original list to be transformed.



Solution 1:[1]

Replace all the zeros in the data array with nan values.

import numpy as np
data = [[1, 2, 3, 4], [0, 2, 3, 4], [0, 0 , 3, 4], [0, 0, 0, 4]] 
data = np.array(data)
data = np.where(data == 0, np.nan, data)

Solution 2:[2]

You can first convert the list to array and then use the np.where. Here is:

import numpy as np
data = [[1, 2, 3, 4], [0, 2, 3, 4], [0, 0 , 3, 4], [0, 0, 0, 4]]
data_arr = np.array(data)
data = np.where(data_arr == 0, np.nan, data)
## change the format as the format you want
out = [list(data[i]) for i in range(data_arr.shape[1])]
out

Solution 3:[3]

The question indicates that either NaN or null are options. However, there isn't really a null value in Python although there is None. So, assuming that None is acceptable then:

data = [[1, 2, 3, 4], [0, 2, 3, 4], [0, 0, 3, 4], [0, 0, 0, 4]]

data = [[x if x else None for x in d] for d in data]

print(data)

Output:

[[1, 2, 3, 4], [None, 2, 3, 4], [None, None, 3, 4], [None, None, None, 4]]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Sadcow
Solution 3 Albert Winestein