Python Pandas 应用值

Python Pandas apply value

我有一个包含几列的 df:'hour'、'day'、'week'、'month'、'year' 和 'value'。我按 'week' 和 'hour' 与 'value' 分组寻找 mean():

df_group = df.groupby(['week','hour']).value.mean().reset_index()

现在我想将该平均值应用为每周每个小时的单独列。有任何想法吗? 提前致谢!

如果需要向原始数据添加新列,我认为您需要 transform

df['new'] = df.groupby(['week','hour']).value.transform('mean')