0%

weighted average pandas

✔️ 如果lambda函数令人困惑,则apply也可以与函数定义一起使用。 (还有一个函数numpy.average计算加权平均值)

1
2
3
4
5
6
7
import numpy as np
def weighted_average(group):
weights = group['Volume']
height = group['Height']
return np.average(height,weights=weights)

df.groupby(['STAND_ID','Species']).apply(func = weighted_average).unstack()