Python 熊猫:如何用平均值填充缺失的数据?
我每隔5秒从远程设备读取一些数据 它们保存为:Python 熊猫:如何用平均值填充缺失的数据?,python,pandas,pandas-resample,Python,Pandas,Pandas Resample,我每隔5秒从远程设备读取一些数据 它们保存为: 2018-01-01 00:00:00 2 2018-01-01 00:00:05 3 2018-01-01 00:00:10 3 2018-01-01 00:00:15 2 2018-01-01 00:00:20 3 2018-01-01 00:00:25 4 2018-01-01 00:00:30 3 2018-01-01 00:00:35 2 2018-01-01 00:00:
2018-01-01 00:00:00 2
2018-01-01 00:00:05 3
2018-01-01 00:00:10 3
2018-01-01 00:00:15 2
2018-01-01 00:00:20 3
2018-01-01 00:00:25 4
2018-01-01 00:00:30 3
2018-01-01 00:00:35 2
2018-01-01 00:00:40 4
2018-01-01 00:00:45 5
2018-01-01 00:00:50 3
2018-01-01 00:00:55 3
2018-01-01 00:00:00 2
2018-01-01 00:00:05 3
2018-01-01 00:00:10 3
.......... 00:00:15 missing...
.......... 00:00:20 missing...
.......... 00:00:25 missing...
2018-01-01 00:00:30 12 <--- sum of the last 4 readings
2018-01-01 00:00:35 2
.......... 00:00:40 missing...
.......... 00:00:45 missing...
2018-01-01 00:00:50 15 <--- sum of the last 3 readings
2018-01-01 00:00:55 3
唉,沟通不是最好的,有时沟通也不起作用
在这种情况下,远程设备将尽快提供数据的累积值
以前的数据可以保存为:
2018-01-01 00:00:00 2
2018-01-01 00:00:05 3
2018-01-01 00:00:10 3
2018-01-01 00:00:15 2
2018-01-01 00:00:20 3
2018-01-01 00:00:25 4
2018-01-01 00:00:30 3
2018-01-01 00:00:35 2
2018-01-01 00:00:40 4
2018-01-01 00:00:45 5
2018-01-01 00:00:50 3
2018-01-01 00:00:55 3
2018-01-01 00:00:00 2
2018-01-01 00:00:05 3
2018-01-01 00:00:10 3
.......... 00:00:15 missing...
.......... 00:00:20 missing...
.......... 00:00:25 missing...
2018-01-01 00:00:30 12 <--- sum of the last 4 readings
2018-01-01 00:00:35 2
.......... 00:00:40 missing...
.......... 00:00:45 missing...
2018-01-01 00:00:50 15 <--- sum of the last 3 readings
2018-01-01 00:00:55 3
但是如何填充NaN并移除峰?
我检查了asfreq
和resample
的各种方法,但在这种情况下没有一种(bfill
,ffill
)是有用的
最终结果应该是:
2018-01-01 00:00:00 2
2018-01-01 00:00:05 3
2018-01-01 00:00:10 3
2018-01-01 00:00:15 3 <--- NaN filled with mean = peak 12/4 rows
2018-01-01 00:00:20 3 <--- NaN filled with mean
2018-01-01 00:00:25 3 <--- NaN filled with mean
2018-01-01 00:00:30 3 <--- peak changed
2018-01-01 00:00:35 2
2018-01-01 00:00:40 5 <--- NaN filled with mean = peak 15/3 rows
2018-01-01 00:00:45 5 <--- NaN filled with mean
2018-01-01 00:00:50 5 <--- peak changed
2018-01-01 00:00:55 3
单向:
m = (read_data.isna() | read_data.shift(fill_value= 0).isna()).astype(int)
read_data = read_data.bfill() / m.groupby(m.ne(m.shift()).cumsum()).transform('count').where(m.eq(1), 1)
输出:
2021-01-01 00:00:00 2.0
2021-01-01 00:00:05 3.0
2021-01-01 00:00:10 3.0
2021-01-01 00:00:15 3.0
2021-01-01 00:00:20 3.0
2021-01-01 00:00:25 3.0
2021-01-01 00:00:30 3.0
2021-01-01 00:00:35 2.0
2021-01-01 00:00:40 5.0
2021-01-01 00:00:45 5.0
2021-01-01 00:00:50 5.0
2021-01-01 00:00:55 3.0
Freq: 5S, dtype: float64
完整示例:
import numpy as np
import pandas as pd
time = pd.date_range(start='2021-01-01', freq='5s', periods=12)
read_data = pd.Series([2, 3, 3, np.nan, np.nan, np.nan, 12, 2, np.nan, np.nan, 15, 3], index=time).dropna()
read_data = read_data.asfreq("5s")
m = (read_data.isna() | read_data.shift(fill_value= 0).isna()).astype(int)
read_data = read_data.bfill() / m.groupby(m.ne(m.shift()).cumsum()).transform('count').where(m.eq(1), 1)
这可以通过分割(分组)将缺失值及其对应峰值(重新采样后)分为一组,回填,然后计算每组的平均值来实现:
>>> read_data = read_data.to_frame(name='val').assign(idx=range(len(read_data)))
>>> read_data = read_data.asfreq('5s').bfill()
>>> read_data = read_data/read_data.groupby('idx').transform(len)
>>> read_data.drop('idx', axis=1, inplace=True)
>>> read_data.val
2021-01-01 00:00:00 2.0
2021-01-01 00:00:05 3.0
2021-01-01 00:00:10 3.0
2021-01-01 00:00:15 3.0
2021-01-01 00:00:20 3.0
2021-01-01 00:00:25 3.0
2021-01-01 00:00:30 3.0
2021-01-01 00:00:35 2.0
2021-01-01 00:00:40 5.0
2021-01-01 00:00:45 5.0
2021-01-01 00:00:50 5.0
2021-01-01 00:00:55 3.0
Freq: 5S, Name: val, dtype: float64
说明:
首先将原始系列转换为dataframe,并引入另一列idx,该列将唯一地将每一行标识为单个组:
>>> read_data = read_data.to_frame(name='val').assign(idx=range(len(read_data)))
>>> read_data
val idx
2021-01-01 00:00:00 2.0 0
2021-01-01 00:00:05 3.0 1
2021-01-01 00:00:10 3.0 2
2021-01-01 00:00:30 12.0 3
2021-01-01 00:00:35 2.0 4
2021-01-01 00:00:50 15.0 5
2021-01-01 00:00:55 3.0 6
重新采样以插入缺失值,然后用峰值重新填充缺失值:
>>> read_data = read_data.asfreq('5s').bfill()
>>> read_data
val idx
2021-01-01 00:00:00 2.0 0.0
2021-01-01 00:00:05 3.0 1.0
2021-01-01 00:00:10 3.0 2.0
2021-01-01 00:00:15 12.0 3.0
2021-01-01 00:00:20 12.0 3.0
2021-01-01 00:00:25 12.0 3.0
2021-01-01 00:00:30 12.0 3.0
2021-01-01 00:00:35 2.0 4.0
2021-01-01 00:00:40 15.0 5.0
2021-01-01 00:00:45 15.0 5.0
2021-01-01 00:00:50 15.0 5.0
2021-01-01 00:00:55 3.0 6.0
正如您现在看到的,回填值与其峰值位于同一组中(具有相同的idx)。
因此,
groupby
idx
只需将值除以每组的长度即可。删除idx列:
我有一个基本但不是很快的解决方案:生成一个完整的日期时间序列,将完整的日期时间列与数据合并,然后知道缺少哪些日期时间。然后使用
if-else
to和flag
决定何时获得平均值。使用for loops
实现这一点。我认为您的解决方案是完美的,唉shift
带来了一个小问题:如果NaN组由单个值分隔,则算法不起作用。尝试更改date\u范围中的periods=11
,并删除原始数据框中12
之后的2
,以了解我的意思。非常感谢您的解决方案。解释得很好。很高兴为您提供帮助。
:)
>>> read_data = read_data/read_data.groupby('idx').transform(len)
>>> read_data.drop('idx', axis=1, inplace=True)
>>> read_data
val
2021-01-01 00:00:00 2.0
2021-01-01 00:00:05 3.0
2021-01-01 00:00:10 3.0
2021-01-01 00:00:15 3.0
2021-01-01 00:00:20 3.0
2021-01-01 00:00:25 3.0
2021-01-01 00:00:30 3.0
2021-01-01 00:00:35 2.0
2021-01-01 00:00:40 5.0
2021-01-01 00:00:45 5.0
2021-01-01 00:00:50 5.0
2021-01-01 00:00:55 3.0