【发布时间】: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 循环后的行没有赋值,为什么会这样?