如何取 NaN 值之间的平均值?

How to take average between NaN values?

我有一个包含数字和 NaN 值的简单列表。有没有办法在两个 NaN 值之间取 AVG?一个例子可能是这样的:

list = [NaN, 5, 6, 7, NaN, NaN, NaN, 6, 2, 8, 5, 4, NaN, NaN]

我希望输出像

Output = [6,5] 

使用 itertools -

中的 groupby
import numpy as np
from itertools import groupby
NaN = np.nan
lst = [NaN, 5, 6, 7, NaN, NaN, NaN, 6, 2, 8, 5, 4, NaN, NaN]
[np.mean(list(g)) for k, g in groupby(lst, key=lambda x: x is not NaN) if k]
# [6.0, 5.0]

不需要额外技巧的简单方法:

import numpy as np

## NaN is assumed to be pre-defined by the users, e.g.: NaN = np.nan or NaN = float('nan')

def get_mean_between_nan(ar):
    out = list()
    t = list()
    for x in ar:
        if np.isnan(x):
            if len(t) > 0:
                out.append(np.mean(t))
                t = list()
        else:
            t.append(x)
    if len(t) > 0:
        out.append(np.mean(t))
    return out

之后,首先将列表拆分为分块的列表列表:

NaN=None # or np.nan, float('nan'), 'nan' or any other separator value you like = even '/'
my_list = [NaN, 5, 6, 7, NaN, NaN, NaN, 6, 2, 8, 5, 4, NaN, NaN]

from itertools import groupby
chunks = list(list(g) for k,g in groupby(my_list, key=lambda x: x is not NaN) if k))
# [[5, 6, 7], [6, 2, 8, 5, 4]]

然后就可以使用内置的statistics.mean()如下:

import statistics
output = [statistics.mean(chunk) for chunk in chunks]
# [6, 5]

给你。

备注:

  1. 不需要numpy。但是如果你确实想要一个纯粹的 numpy 解决方案,你可以使用
  2. 不要使用 list 作为变量名,因为它是内置类型的名称!