Home > Software design >  How to replace specific values in a list with NaN?
How to replace specific values in a list with NaN?

Time:04-19

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.

CodePudding user response:

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)

CodePudding user response:

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

CodePudding user response:

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]]
  • Related