Skip to main content
 首页 » 编程设计

python之查找平均值但忽略列表中的任何零 [Python]

2025年05月04日9sky-heaven

我有一个温度数据文本文件,如下所示:

3438012868.0    0.0 21.7    22.6    22.5    22.5    21.2 
3438012875.0    0.0 21.6    22.6    22.5    22.5    21.2 
3438012881.9    0.0 21.7    22.5    22.5    22.5    21.2 
3438012888.9    0.0 21.6    22.6    22.5    22.5    21.2 
3438012895.8    0.0 21.6    22.5    22.6    22.5    21.3 
3438012902.8    0.0 21.6    22.5    22.5    22.5    21.2 
3438012909.7    0.0 21.6    22.5    22.5    22.5    21.2 
3438012916.6    0.0 21.6    22.5    22.5    22.5    21.2 
3438012923.6    0.0 21.6    22.6    22.5    22.5    21.2 
3438012930.5    0.0 21.6    22.5    22.5    22.5    21.2 
3438012937.5    0.0 21.7    22.5    22.5    22.5    21.2 
3438012944.5    0.0 21.6    22.5    22.5    22.5    21.3 
3438012951.4    0.0 21.6    22.5    22.5    22.5    21.2 
3438012958.4    0.0 21.6    22.5    22.5    22.5    21.3 
3438012965.3    0.0 21.6    22.6    22.5    22.5    21.2 
3438012972.3    0.0 21.6    22.5    22.5    22.5    21.3 
3438012979.2    0.0 21.6    22.6    22.5    22.5    21.2 
3438012986.1    0.0 21.6    22.5    22.5    22.5    21.3 
3438012993.1    0.0 21.6    22.5    22.6    22.5    21.2 
3438013000.0    0.0 21.6    0.0     22.5    22.5    21.3 
3438013006.9    0.0 21.6    22.6    22.5    22.5    21.2 
3438013014.4    0.0 21.6    22.5    22.5    22.5    21.3 
3438013021.9    0.0 21.6    22.5    22.5    22.5    21.3 
3438013029.9    0.0 21.6    22.5    22.5    22.5    21.2 
3438013036.9    0.0 21.6    22.6    22.5    22.5    21.2 
3438013044.6    0.0 21.6    22.5    22.5    22.5    21.2 

但是整个文件要长得多,这是前几行。第一列是时间戳,接下来的 6 列是温度记录。我需要编写一个循环来找到 6 个测量值的平均值,但会忽略 0.0 的测量值,因为这仅意味着传感器未打开。在稍后的测量中,第一列确实有一个测量值。有没有办法让我写一个 if 语句或另一种方法来只找到列表中非零数字的平均值?现在,我有:

time = [] 
t1 = [] 
t2 = [] 
t3 = [] 
t4 = [] 
t5 = [] 
t6 = [] 
newdate = [] 
 
temps = open('file_path','r') 
sepfile = temps.read().replace('\n','').split('\r') 
temps.close() 
 
for plotpair in sepfile: 
    data = plotpair.split('\t') 
    time.append(float(data[0])) 
    t1.append(float(data[1])) 
    t2.append(float(data[2])) 
    t3.append(float(data[3])) 
    t4.append(float(data[4])) 
    t5.append(float(data[5])) 
    t6.append(float(data[6])) 
 
for data_seconds in time: 
    date = datetime(1904,1,1,5,26,02) 
    delta = timedelta(seconds=data_seconds) 
    newdate.append(date+delta) 
 
for datapoint in t2,t3,t4,t5,t6: 
    temperatures = np.array([t2,t3,t4,t5,t6]).mean(0).tolist() 

它只找到最近 5 次测量的平均值。我希望找到一种更好的方法来忽略 0.0,并在第一列为非 0 时包含它。

请您参考如下方法:

之前的问题表明您已经安装了 NumPy。因此,使用 NumPy,您可以将零设置为 NaN,然后​​调用 np.nanmean 来获取平均值,忽略 NaN:

import numpy as np 
 
data = np.genfromtxt('data') 
data[data == 0] = np.nan 
means = np.nanmean(data[:, 1:], axis=1) 

产量

array([ 22.1  ,  22.08 ,  22.08 ,  22.08 ,  22.1  ,  22.06 ,  22.06 , 
        22.06 ,  22.08 ,  22.06 ,  22.08 ,  22.08 ,  22.06 ,  22.08 , 
        22.08 ,  22.08 ,  22.08 ,  22.08 ,  22.08 ,  21.975,  22.08 , 
        22.08 ,  22.08 ,  22.06 ,  22.08 ,  22.06 ])