遍历Panda行组,并根据组平均值和标准差过滤异常值



我有一列由100个样本组成的数据。我试图排除一些不符合组平均值和标准偏差的样本。所以,我有10组连续的数据,有10个平均值和标准偏差值。最后,我想根据每个组的平均值和标准偏差值,从每个组的数据中删除异常值。

这是我的代码:

Ch_37['MA']=Ch_37['Rssi-1'].rolling(window=10,center=False).mean() # moving average of 10 samples
Ch_37['std']=Ch_37['Rssi-1'].rolling(window=10,center=False).std() # moving standard deviation of 10 samples
ave= Ch_37['MA'].iloc[9::10].reset_index(drop=True) # filter the average to form 10 group
std = Ch_37['std'].iloc[9::10].reset_index(drop=True)
# remove outlier from each group based on group mean and standard deviation
new_rssi_ch_37=Ch_37['Rssi-1'].iloc[::10].between(ave.iloc[::1].sub(std.iloc[::1].mul(1)),
ave.iloc[::1].add(std.iloc[::1].mul(1)),
inclusive=False)
# Some of the data samples are shown below
-37
-49
-52
-69
-42
-50
-46
-34
-37
-59
-61
-72
...

我一直在思考迭代每个组并提取值

我添加了下面的逻辑,虽然它看起来不是一个优雅的解决方案,但暂时可以正常工作。在每个步长为10之后,for循环打印列名和数据类型,我不能忽略它们。

for i in range(0,len(Ch_37),10):
if k>=len(ave):
break
mask=Ch_37['Rssi-1'].loc[i:i+9].between(ave[k]-std[k],
ave[k]+std[k],
inclusive='both')   #.loc[i:i+9:1]

print(Ch_37['Rssi-1'].loc[i:i+9][mask])
k=k+1

最新更新