熊猫:结合重采样和分组依据。在将它们汇总到一个时间段之前,如何在同一组中求平均值?

我有与时间戳和自定义组关联的表格数据(由col1,col2等描述)。例:

enter image description here

I would like to create time bins (like with resample function), average values WITHIN the same group, and THEN sum them up within the same time bin. But if I write dataframe.resample("2D").sum(), values will be direclty summed up over all the groups without first being averaged in the same group. Like this:

enter image description here

但是我需要的是在求和之前对属于同一组的值求平均值。在这种情况下,所需的输出将是:

enter image description here

I tried to apply groupby after resampling (e.g. dataframe.resample("2D").grouby("Group")), but it raises a TypeError ("'TimeGrouper' object is not callable")

有什么办法可以解决这个问题?提前致谢。

评论
  • desse
    desse 回复

    I believe you can first aggregate mean and then use resample:

    df = (dataframe.groupby(['Time','Group'])
                   .mean()
                   .reset_index(level=1, drop=True)
                   .resample("2D")
                   .sum())