【问题标题】:Python Beginner: Removing nan form an array of arraysPython初学者:从数组数组中删除nan
【发布时间】:2020-11-05 20:12:53
【问题描述】:

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

我不想删除整个数组,只删除 nan 对象。

我使用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

的示例数组
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])

我已经包含了数组数组的示例

【问题讨论】:

  • 欢迎来到 SO !您是否考虑使用此处记录的 np.nanmean numpy.org/doc/stable/reference/generated/numpy.nanmean.html ?最佳
  • 谢谢!是的,我确实尝试过,但它仍然通过 - 这就是我考虑进行过滤的原因
  • @KristianBonderup - for 循环后的行没有赋值,为什么会这样?

标签: python arrays numpy nan


【解决方案1】:

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

# 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。

【讨论】:

    猜你喜欢
    • 2020-08-10
    • 1970-01-01
    • 2020-04-21
    • 2012-07-22
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 2017-03-26
    相关资源
    最近更新 更多