首页 > 解决方案 > Python初学者:从数组数组中删除nan

问题描述

所以我正在使用一个数组数组,我想计算这个数组中每个数组的平均值,但是有些数组有 nan 值。

我不想删除整个数组,只是 nan obejct。

我使用 numpy,数组的类型是 float64

我在这里尝试了一些解决方案,但没有运气。

values = np.split(dflow['fixf'].to_numpy(), np.where(np.diff([int(elem[3:]) for elem in dflow['ftid']]))[0] + 1) # creating the array of arrays

final = []
for index, value in enumerate(values):
    [[i for i in j if not np.isnan(x)] for j in values] # attempt to remove the nan values via iteration (did not work)
    final.append(value.mean()) # calculating the mean

analysis = pd.DataFrame({'value': final, 'hour': np.concatenate([[x for x in range(24)] for y in range(7)]),
                         'day': np.concatenate([np.full(24, index) for index in range(7)])}) # the mean should end up in the list final.

下面是值的示例数组

values [array([11.26034969, 12.9716698 , 12.9716698 , 12.9716698 , 12.9716698 , 12.9716698 , 12.9716698 , 12.9716698 , 12.9716698 ]), array([2.19253936, 2.83246649, 2.83246649, 2.83246649, 2.83246649, 2.83246649, 2.83246649, 2.83246649, 2.83246649]), array([0., 0., 0., 0., 0., 0., 0., 0., 0.]), array([0., 0., 0., 0., 0., 0., 0., 0., 0.]), array([0., 0., 0., 0., 0., 0., 0., 0., 0.]), array([nan, -0.73396306, -0.73396306, -0.73396306, -0.73396306, -0.73396306, -0.73396306, -0.73396306, -0.73396306])

我已经包含了一个数组数组的样本

标签: pythonarraysnumpynan

解决方案


这只是一个快速的。那里可能有更好的解决方案。

# this was the array of arrays  you posted
# I have used np.nan to produce nan values
# and np.array to build the each array element again 
myarray = np.array([ np.array([np.nan, 12.9716698 , 12.9716698 , 12.9716698 , 12.9716698 , 12.9716698 , 12.9716698 , 12.9716698 , 12.9716698 ]), np.array([2.19253936, 2.83246649, 2.83246649, 2.83246649, 2.83246649, 2.83246649, 2.83246649, 2.83246649, 2.83246649]), np.array([0., 0., 0., 0., 0., 0., 0., 0., 0.]), np.array([0., 0., 0., 0., 0., 0., 0., 0., 0.]), np.array([0., 0., 0., 0., 0., 0., 0., 0., 0.]), np.array([np.nan, -0.73396306, -0.73396306, -0.73396306, -0.73396306, -0.73396306, -0.73396306, -0.73396306, -0.73396306])])

# now you could get a mask of values that are not nan in your array
noNanValues= [~np.isnan(i) for i in myarray  ]

# use the mask to remove nan values from your array and save as list
newArray =  [j[noNanValues[i]] for i,j in enumerate(myarray)] 
# if you want back an array 
newArray2 =  np.array([newArray])


# check the first array to ensure the Nan has been removed
newArray[0].shape
newArray2[0][0].shape

# compute mean and other desired operation

您还可以考虑将数组导入数据框并在那里方便地处理 Nan。


推荐阅读