我有一个数据库,其中混合了每分钟、每 5 分钟和每小时的数据点: 我的目标是获得 10 分钟的数据平均值,但是当我绘制此图时,缺少数据点,并且写入的 CSV 文件从写入每个数据点变为每小时写入)
输出看起来像
2005-03-01 17:00:00,3.25
2005-03-01 17:10:00,-5.75
2005-03-01 17:20:00,-6.0
2005-03-01 17:30:00,
2005-03-01 17:40:00,
2005-03-01 17:50:00,
2005-03-01 18:00:00,2.3
2005-03-01 18:10:00,
2005-03-01 18:20:00,
2005-03-01 18:30:00,
2005-03-01 18:40:00,
2005-03-01 18:50:00,
2005-03-01 19:00:00,2.8
原始输入,如下所示:
01-mar-05 17:10, 1.6, 7.9, 0.0214, 1.3536, 0.0214, 1.6726, 1.00,30.567
01-mar-05 17:15, -13.1, 7.9, 0.0214, 1.3540, 0.0214, 1.6729, 1.00,30.550
01-mar-05 17:20, 3.2, 7.9, 0.0214, 1.3542, 0.0214, 1.6731, 1.00,30.554
01-mar-05 17:25, -15.2, 7.9, 0.0214, 1.3544, 0.0214, 1.6731, 1.00,30.534
01-mar-05 18:00, 2.3, 8.0, 0.0214, 1.8276, 0.0214, 1.6932, 1.00, 0.034
01-mar-05 19:00, 2.8, 8.0, 0.0214, 1.8312, 0.0214, 1.6973, 1.00, 0.081
01-mar-05 20:00, 6.8, 8.0, 0.0214, 1.8313, 0.0214, 1.6993, 1.00, .192
我使用的代码是:
names= ['Date','Conc','Flow','SZ','SB','RZ','RB','Fraction','Attenuation']
df = pd.read_csv('Output13.csv', index_col=0, names=names, parse_dates=True)
df1 = df[['Conc']].resample('10min').mean()
我尝试过
df=df.resample('1min',fill_method='bfill')
认为这会填充原始文件中的所有数据点...但它不起作用。
有什么建议吗? 谢谢!
最佳答案
当你最后每小时只能获得一次读数时,你的数据有点稀疏,无法每 10 分钟获得点...由于你得到了丢失的点,你要么必须使用你拥有的数据(使用 ffill
或 bfill
),或插入缺失的数据。
df['Conc'].plot(label='original')
df['Conc'].resample('10T').ffill().plot(label='ffill')
df['Conc'].resample('10T').bfill().plot(label='bfill')
df['Conc'].resample('10T').mean().interpolate(method='linear').plot(label='linear interpolation')
df['Conc'].resample('10T').mean().interpolate(method='cubic').plot(label='cubic interpolation')
plt.legend(loc=4)
关于python - 尝试对 Pandas 进行上采样以获得每分钟的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37946847/