Pandas 使用加权平均值对数据帧中的数据重新采样
我有一个基于时间的数据帧,我需要按12小时和一天的周期重新采样。到目前为止,我使用的代码如下:Pandas 使用加权平均值对数据帧中的数据重新采样,pandas,dataframe,Pandas,Dataframe,我有一个基于时间的数据帧,我需要按12小时和一天的周期重新采样。到目前为止,我使用的代码如下: 如果self.resample_by=='day': self.model_df=self.model_df.resample('D',on='index').mean() self.model_df.dropna(轴=0,原地=True) elif self.resample_by==“shift”: delta=dt.timedelta(小时=12) self.model_df=self.mode
如果self.resample_by=='day':
self.model_df=self.model_df.resample('D',on='index').mean()
self.model_df.dropna(轴=0,原地=True)
elif self.resample_by==“shift”:
delta=dt.timedelta(小时=12)
self.model_df=self.model_df.resample(delta,on='24h_day_start')。平均值()
self.model_df.dropna(轴=0,原地=True)
其他:
通过
现在,正如您所看到的,我正在对重采样数据使用mean()
作为权宜之计,但实际上不同的行具有不同的权重,因此我需要进行加权平均计算
据我所知,我需要编写一个函数来计算加权平均值,然后使用apply(func)
而不是mean()
。对吗
然而,我不明白的是如何构造我的函数,因为我对DataFrame.resample
实际返回的内容感到困惑。表示该函数返回一个重采样对象。有人知道这个对象是什么,以及我如何在加权平均函数中使用输出吗
或者我可以使用另一种方法?迭代
组
属性
rs = df.resample(...)
for group_name, group_labels in rs.groups.items():
vals = rs.get_group(group_name)
# apply weighted average function to vals df
有关重采样对象的完整文档,请访问谢谢您提供的信息!这很有魅力!